Acquire control in TemplateField by Id.

  • Thread starter Thread starter zlf
  • Start date Start date
Z

zlf

Hello,
I have a GridView with TemplateField. I want to get the <asp:TextBox
ID="TextBox1" runat="server"></asp:TextBox> in TemplateField in programmatic
way. e.g: GridView1.TextBox1 or GridView1.FindControl("TextBox1"), but
neither of them works. Please help me.


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Employeeid"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:TemplateField HeaderText="link">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Thanks

zlf
 
Hello,
I have a GridView with TemplateField. I want to get the <asp:TextBox

I have a suspicion that what you'd want to do would be more
interesting than
HOW to do it...

Why do you need the Control?
Is it to extract some kind of ID?

Check out this sample;
http://ajaxwidgets.com/AllControlsSamples/CalendarWebApplicationWithDateTimePicker.aspx
(Click the "Show Code for this sample")

Sure it's epcific for Gaia Ajax Widgets, but still it's the exact same
syntax with conventional APS.NET...


..t
 
Actually, the control I want to get is not a TextBox, it is custom control.
And I need to assign some special property of it to make it display
specified data.
 
This is probably what you were looking for.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
TextBox c = (TextBox) e.Row.FindControl("TextBox1");
if (c!=null)
{
// set properties
}
else
{
// did not find or was binding Footer/Headers etc.
}
Int32 d = 0;
}

YUou also can bind properties declaratively using Eval or Container.DataItem
 
Back
Top