How to get value of a specific column in OnRowDataBound function?

2.8k Views Asked by At

I have a dropdown and a gridview. Depending on the value selected in the dropdown, I want to update the NaviagetURL for the hyperlink specified in 1st column of the gridview. So I am using onRowDataBound function to update the hyperlink's navigateurl dynamically. And, I am trying to retrieve value of first column "ID" to use it as the query string inside the OnRowDataBound function but I am not able to get this working.

When I click on the hyperlink in the first column, the query string doesn't have any value assigned to the variable ID. So, the url just resolves to http://localhost/JobCosting/EditSavedTicket.aspx?ID=

What is expected is, the url which opens on clicking the hyperlink should have the value specified in 1st column assigned to the variable ID in the query string. If I use e.Row.Cells[1].Text or e.Row.Cells[2].Text, the column value is being retrieved correctly, but I need the value in column zero which is not getting retrieved. When I check the length of the string id which is e.Row.Cells[0].Text, it is zero

GridView:

<asp:GridView ID="GridView2" runat="server" AllowSorting="True" 
 AutoGenerateColumns="False" DataSourceID="Saved_Work" OnRowDataBound="update_url" 
 Style="float: left; font-size: small; position: relative;
            width: 82%; position: relative; top: -10px; height: 40px; font-size: small; text-align: left;
            left: 30px;" align="left">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:HyperLink ID="Continue_SavedWork" runat="server" NavigateUrl='<%# Eval("ID", "~/EditSavedJob.aspx?ID={0}") %>'
                            Text='<%# Eval("ID") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="UserID" HeaderText="User" SortExpression="UserID" />
                <asp:BoundField DataField="Date_Created" HeaderText="Last_Updated_On" SortExpression="Date_Created" />
            </Columns>
        </asp:GridView>

Code behind:

protected void update_url(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //string NavigateUrl="<%# Eval('ID', 'EditSavedTicket.aspx?ID={0}) %>";
        string id = e.Row.Cells[0].Text;
        HyperLink hp = (HyperLink)e.Row.FindControl("Continue_SavedWork");
        if (SavedWorkDD.SelectedValue.ToString() == "Tickets")
            hp.NavigateUrl = string.Format("EditSavedTicket.aspx?ID={0}", id);
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

I suspect that the reason why you can't access the text of the first column is that it is a template column whereas the other columns are BoundField columns.

Instead of retrieving the text from the controls, you can access the DataItem in your OnRowUpdated event handler. For instance, if you are binding to a DataTable, you can access the DataRowView like this:

protected void update_url(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    var row = (DataRowView)e.Row.DataItem;
    var id = (int)row["ID"];
    // ...
  }
}

This approach is more reliant than trying to find the controls and accessing their values. It will continue to work even if you change the UI.