Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to find id using Hiddenfield in Gridview on button click & button is outside Gridview C# I am trying but its showing error "Object reference not set to an instance of an object." on "string strhfId = (gvRow.FindControl("hfId") as HiddenField).Value;"

I have tried different too
HiddenField hfId = (HiddenField)gridData.SelectedRow.Cells[0].FindControl("hfId");

but error is same


What I have tried:

protected void lnkUpdate_Click(object sender, EventArgs e)
        {
            //HiddenField hfId = (HiddenField)gridData.SelectedRow.Cells[0].FindControl("hfId");
            
         
            //HiddenField strId = ((HiddenField)gridData.Rows[0].FindControl("hfIdValue"));
            //String value = strId.Value;
          
          
            LinkButton btn = sender as LinkButton;
            GridViewRow gvRow = btn.NamingContainer as GridViewRow;
            string strhfId = (gvRow.FindControl("hfId") as HiddenField).Value;
                try
                {
                   

                    //HiddenField hfIdc = (HiddenField)gridData.SelectedRow.Cells[0].FindControl("hfIdValue");
                    List<SqlParameter> parameters = new List<SqlParameter>
                    {
                    };
                    parameters.Add(new SqlParameter("@P_APPLICATION_ID", strhfId));
                    parameters.Add(new SqlParameter("@P_APPLICATION_NAME", txtApplicationName.Text));
                
                    parameters.Add(new SqlParameter("@P_MODE", "E"));

                    string strResponse = objAppFunctionModule.ExecuteNonQueryMethod(strUSP_APPLICATION_MST, parameters);
                 
                    
                        PopulateGrid();
                   
             
                }


                catch (Exception ex)
                {
                    lblResponse.Text = ex.Message;
                }
            
        }



Markup;

<asp:GridView ID="gridData" runat="server" AutoGenerateColumns="False" OnRowDataBound="gridData_RowDataBound"
                                        RowStyle-HorizontalAlign="Center" ShowHeaderWhenEmpty="true" OnRowCommand="gridData_RowCommand"
                                        EmptyDataText="No Data for the Selected Value" BackColor="White" BorderColor="#c5c5c5" BorderWidth="1px" Width="100%"
                                        CellPadding="4" AllowPaging="True">
                                        <columns>
                                            <asp:templatefield>
                                                <ItemStyle />
                                                <HeaderStyle CssClass="ItemStyle" />
                                                <headertemplate>
                                                    <asp:CheckBox ID="chkAll" class="cbAll" runat="server" onclick="javascript:SelectAllCheckboxes1(this);" />
                                                
                                                <itemtemplate>
                                                    <asp:CheckBox ID="chkMultiDelete" runat="server" />
                                                
                                            

                                            <asp:TemplateField HeaderText="Application Name">
                                                <ItemStyle />
                                                <HeaderStyle CssClass="ItemStyle" />
                                                <itemtemplate>
                                                    <asp:Label ID="lblApplicationName" runat="server" Text='<%#Eval("APPLICATION_NAME") %>'>
                                                    <asp:HiddenField runat="server" ID="hfId" Value='<%#Eval("APPLICATION_ID")%>'>
                                                
                                                                                 
                                            <asp:TemplateField HeaderText="Edit">
                                                <ItemStyle />
                                                <HeaderStyle CssClass="ItemStyle" />
                                                <itemtemplate>
                                                    <asp:LinkButton ID="lnkEdit" class="btn btn-default" CommandName="EditButton" CommandArgument='<%# ((GridViewRow) Container).RowIndex + ";" %>' runat="server">class="fa fa-pencil">Edit
Posted
Updated 1-Feb-18 23:36pm
v2
Comments
F-ES Sitecore 2-Feb-18 4:18am    
You'll need to post the mark-up as well, it's unclear where this hidden field is. If the hidden field is asp:Hidden and it's outside of the gridview then you should be able to reference it like normal

hfId.Value
Pahi2317 2-Feb-18 4:43am    
<asp:GridView ID="gridData" runat="server" AutoGenerateColumns="False" OnRowDataBound="gridData_RowDataBound"
RowStyle-HorizontalAlign="Center" ShowHeaderWhenEmpty="true" OnRowCommand="gridData_RowCommand"
EmptyDataText="No Data for the Selected Value" BackColor="White" BorderColor="#c5c5c5" BorderWidth="1px" Width="100%"
CellPadding="4" AllowPaging="True">
<columns>
<asp:templatefield>
<ItemStyle />
<HeaderStyle CssClass="ItemStyle" />
<headertemplate>
<asp:CheckBox ID="chkAll" class="cbAll" runat="server" onclick="javascript:SelectAllCheckboxes1(this);" />

<itemtemplate>
<asp:CheckBox ID="chkMultiDelete" runat="server" />



<asp:TemplateField HeaderText="Application Name">
<ItemStyle />
<HeaderStyle CssClass="ItemStyle" />
<itemtemplate>
<asp:Label ID="lblApplicationName" runat="server" Text='<%#Eval("APPLICATION_NAME") %>'>
<asp:HiddenField runat="server" ID="hfId" Value='<%#Eval("APPLICATION_ID")%>'>


<asp:TemplateField HeaderText="Edit">
<ItemStyle />
<HeaderStyle CssClass="ItemStyle" />
<itemtemplate>
<asp:LinkButton ID="lnkEdit" class="btn btn-default" CommandName="EditButton" CommandArgument='<%# ((GridViewRow) Container).RowIndex + ";" %>' runat="server">Edit
Richard Deeming 2-Feb-18 10:10am    
"... button is outside gridview ..."
"btn.NamingContainer as GridViewRow"

If the button is outside of the GridView, then why are you assuming that its naming container will be the GridViewRow?

Hi,

The idea is to fill your hiddenField when user select a row from the gridview.
First you should add the SelectedIndexchanged event in the Gridview Tag :
<asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="false" OnSelectedIndexChanged = "OnSelectedIndexChanged">

you code behind should look like this :
C#
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    //Accessing the id field using  Column index : i supposed your index is the 
    //first column here "0"
    string id = GridView1.SelectedRow.Cells[0].Text;
    //Or
    //you can aslo access the field by its name : make sure to specify the column 
    //type (label in this example)
    string id = (GridView1.SelectedRow.FindControl("id") as Label).Text;
    //bind the value to the hidden field
    hfId.Text = id; //the hidden filed is not related to Gridview, its just a html 
    //tag with hfId as its Id 

}

once the user click on the input button, you just have to get the hidden field value
C#
protected void lnkUpdate_Click(object sender, EventArgs e)
        {
int selectedId =  Convert.toInt32(hfId.Text); //this is a little risky since the //hfText value can be a non Int. better use a TryParse.
}

If i misunderstood, and the Hidden field is in the gridview, everything showen earlier is still valid, you just have to fill a global variable instead of the hidden field (when user select a row), and access it when the user click the linkbutton.

Hope it helps.
 
Share this answer
 
Change the commandargument to drop the ";"

CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'


Then hook into the grid's command event rather than the link button's click event

protected void gridData_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridView g = (GridView)sender;

    int index = int.Parse(e.CommandArgument.ToString());

    GridViewRow row = g.Rows[index];
    HiddenField hfId = (HiddenField) row.Cells[1].FindControl("hfId");
}


You're probably better using the gridview's inbuilt editable row framework though.
 
Share this answer
 
hi,
not working again the same error
"
Object reference not set to an instance of an object.
"
 
Share this answer
 
Comments
Richard Deeming 2-Feb-18 10:07am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your comment as a new "solution".
hi....
I want Hiddenfield value on button click which is outside the gridview not in RowCommand event...
please help me out
 
Share this answer
 
Comments
F-ES Sitecore 2-Feb-18 5:44am    
Stop posting comments\questions as solutions, this isn't a forum discussion format. As long as you get the data you need what does it matter which event you use?

protected void lnkEdit_Click(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;

int index = int.Parse(l.CommandArgument.ToString());

GridViewRow row = gridData.Rows[index];
HiddenField hfId = (HiddenField)row.Cells[1].FindControl("hfId");
}
Pahi2317 2-Feb-18 5:55am    
Hii,
int index = int.Parse(l.CommandArgument.ToString());
error in this line
"Input string was not in a correct format."
i am not getting how do i correct that? Please help me out..its much needed
F-ES Sitecore 2-Feb-18 6:20am    
Look at first comment in Solution 3.
Pahi2317 2-Feb-18 6:38am    
Hii, I did but nothing happen again same error of correct foramt.
F-ES Sitecore 2-Feb-18 6:39am    
What is in "l.CommandArgument"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900