TheBigRedCup
TheBigRedCup

Reputation: 11

Can't access control in footer of ASP.Net Gridview

By company mandate. I have a Gridview with a footertemplate, and in that template I have a textbox.

When I go to access it in code behind, it's coming up not found. Is this some kind of scope issue? Shouldn't the code behind have access to all the fields in my gridview?

<FooterTemplate>
    <asp:TextBox Name="txtID" ControlID="cntID" Width="20" runat="server"></asp:TextBox>
</FooterTemplate>

.

insert.Parameters.AddWithValue("@id", txtID not found .....

Upvotes: 0

Views: 666

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415810

When you nest one control inside another, it's best to use the parent control's FindControl() method. Also, you need to give the control an ID attribute, not just a name.

<FooterTemplate>
    <asp:TextBox ID="txtID" ControlID="cntID" Width="20" runat="server"></asp:TextBox>
</FooterTemplate>

.

insert.Parameters.Add("@id", SqlDbType.NVarchar, 50).Value = Gridview1.FooterRow.FindControl("txtID").Text

And if you're curious, here's why I switched this away from AddWithValue()

Upvotes: 1

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14604

You need to give ID to your textbox, you cannot access contorls by Name in code behind.

asp:TextBox  ID="txtID" ControlID="cntID" Width="20" runat="server"></asp:TextBox>

Upvotes: 0

Related Questions