Click here to Skip to main content
15,894,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a grid named gvDetails. Its 'Item Template' contains a panel named pnlCustomer, inside the panel i have created a textbox- txtCustName . Now I want to fetch the value of texbox in aspx.cs page.
At present i am trying to fetch using the following codes:
Method 1:

C#
Panel pnlCustomer= (Panel)gvDetails.FindControl("pnlCustomer");
TextBox txtCustName = (TextBox )pnlCustomer.FindControl("txtCustName ");


Method 2:
C#
TextBox txtCustName = (TextBox )gvDetails.FindControl("txtCustName");


but this generates following error:"Object reference not set to an instance of an object."
Posted

the best way to find your control make debugging of ur code and
use quickwatch by right clicking on grid object
 
Share this answer
 
Comments
pryashrma 6-Nov-12 0:42am    
please explain
pryashrma 6-Nov-12 0:44am    
i want to use the textbox value further, so could you plz explain how can i store its value in a string variable in .aspx.cs page
pryashrma 6-Nov-12 4:57am    
Thanks for trying
 
Share this answer
 
Comments
pryashrma 6-Nov-12 4:57am    
Thanks for trying
in aspx page

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Add1" HeaderText="Add1" />
        <asp:BoundField DataField="Add2" HeaderText="Add2" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Panel ID="pnlCustomer" runat="server">
                    <asp:TextBox runat="server" ID="txtCustName"></asp:TextBox>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField>
            <ItemTemplate>
              <asp:Button text="click" runat="server" ID="b1" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>




In codbehind page


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable("tblTest");
DataRow dr;

dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Add1", typeof(string));

dt.Columns.Add("Add2", typeof(string));
dr = dt.NewRow();

dr["Name"] = "Neha";
dr["Add1"] = "Address1";

dr["Add2"] = "Add 2";
dt.Rows.Add(dr);

GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int index = row.RowIndex;
//((TextBox)GridView1.Rows[index].Cells[3].Controls[1])
string strName = ((TextBox)((Panel)GridView1.Rows[index].Cells[3].Controls[1]).Controls[1]).Text.ToString();
Response.Write(strName);
}






You can write the below code in case of "btnAddDetails_Click" but here u have to preserve index value.

protected void btnAddDetails_Click(object sender, EventArgs e)
{
string strName = ((TextBox)((Panel)GridView1.Rows[0].Cells[3].Controls[1]).Controls[1]).Text.ToString();
Response.Write(strName);
}



do Let me know if it worked
 
Share this answer
 
v2
Comments
pryashrma 6-Nov-12 4:56am    
Thanks for the help! it worked. The problem was that i was missing the RowIndex of the Selected Row
NAPorwal(8015059) 6-Nov-12 5:15am    
ur welcome plz mark it as solved ,so that it can be useful for others in future
In wrote the same code in GridView1_RowCommand event, just included the RowIndex like this

C#
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int index = row.RowIndex;
pnlCustomer= (Panel)gvDetails.Rows[index].FindControl("pnlCustomer");
txtCustName = (TextBox)pnlRequestPopUp.FindControl("txtCustName ");
 
Share this answer
 

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