views:

199

answers:

3

I put a hyperlinkfield into a gridview, but I realise sometimes I want it to be clickable and sometimes not, depending on the data.

If the item is A or B, I want a hyperlink to bibble.aspx?id=123 , otherwise I just want plain text.

What's the best way? Should I be using another type of field for this?

+1  A: 

You're probably better off with a template field and a hyperlink control, with the NavigateUrl determined by a ternary operator.

ScottE
Thanks do do do
SLC
A: 

You need to handle the RowDataBound event of your GridView.

This link demonstrates how to use the RowDataBound event to modify the value of a field in the data source before it is displayed in a GridView control.

JohnIdol
Yeah I have just done this and am about to add a hyperlink... the only problem now I just hit is how to get the ID to append to the hyperlink url. It's not displayed as a field (although it makes up part of the hyperlink in another field), although it is pulled back into the datasource. How can I extract it in code? Usually I would just use the datakeyname property. It might be obvious, I am still new to gridviews;D
SLC
I was off to lunch - looks like you got your solution in the meanwhile :)
JohnIdol
+2  A: 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
     onrowdatabound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Column to check">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCrtl" Text='<%# Eval("Name") %>' />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Column Name">

        </asp:TemplateField>
    </Columns>
</asp:GridView>



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // you may need to do this if you didnt use templatefield;
        // string val = e.Row.Cells[<column index>].Text;
        // if its templatefield do following;
        Label lbl = e.Row.FindControl("lblCrtl") as Label; 

        Button btn = null;

        if (lbl.Text == "Car") // put your own value to check, my case it was Car
        {
            btn = new Button();
            btn.Text = "Test";
            e.Row.Cells[1].Controls.Add(btn); // cells<column index that control will be added>
        }
    }
}
Cem
I tried something similar to this, with a templatefield, but for some reason e.Row.Cells[7].Text is "" which can't be right...
SLC
if you used templatefield you cant access the control like e.Row.Cells[7].Text, you rather use e.Row.FindControls("ctrlID"); and use its properties depending what control you used.
Cem
Thanks, the problem is I want either a label, or a hyperlink. So should I put the label into the templatefield, then in the codebehind, remove it and add a hyperlink? Also wouldn't giving it an ID cause more than one label with the same ID on the page?
SLC
where is the control which helps you to decide if its label or hyperlink?
Cem
review the code above again, modified it
Cem
It's actually the data that comes back in that field. It's a status for something, and if the status is set to "None", it should be a hyperlink, otherwise it will just be plain text.
SLC
((System.Data.Common.DbDataRecord) e.Row.DataItem)["Status"]=="None"
Cem