Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can I convert the sample html code below into C#? I have searched around but could not find any usable help.

ASP.NET
<asp:TemplateField HeaderText="ID">

<ItemTemplate>
<asp:UpdatePanel runat="server" ID="UpId" 
UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:TextBox ID="TxtId" runat="server" 
Text="" OnTextChanged="TxtId_TextChanged" 
AutoPostBack="true"></asp:TextBox>
</ContentTemplate>

</asp:UpdatePanel>
</ItemTemplate>

</asp:TemplateField>


I have tried this so far, but have no idea how to get the UpdatePanel into Templatefield.
C#
TemplateField tpfield = new TemplateField();
tpfield.HeaderText = "ID"
gv.Columns.Add(tpfield);

UpdatePanel u1 = new UpdatePanel();
TextBox tb1 = new TextBox();
tb1.AutoPostBack = true;
tb1.TextChanged += tb1_TextChanged;

u1.ContentTemplateContainer.Controls.Add(tb1);
Posted
Updated 3-Nov-14 22:24pm
v2
Comments
DamithSL 4-Nov-14 3:57am    
can you update the question with the code what you have tried so far?
Member 11204763 4-Nov-14 4:25am    
Done.
DamithSL 4-Nov-14 4:40am    
You are create gridview controls, update panel and child controls dynamically and also you have to fire events on dynamically added controls. To do this, you need to be fully aware about ASP.NET page life cycle. check below thread for more information and how much its complex. dynamic-created-controls-inside-updatepanel[^] if you can explain the requremnent there may be a possiblity to find alternative way.

1 solution

You need to create a class which implements the ITemplate interface[^]. The InstantiateIn method[^] will be called with the container control, and your class should add the template controls at that point.
C#
sealed class IDFieldTemplate : ITemplate
{
    public EventHandler TextChangedHandler;
    
    public void InstantiateIn(Control container)
    {
        UpdatePanel u1 = new UpdatePanel();
        TextBox tb1 = new TextBox();
        tb1.AutoPostBack = true;
        tb1.TextChanged += TextChangedHandler;
        
        u1.ContentTemplateContainer.Controls.Add(tb1);
        container.Controls.Add(u1);
    }
}
...

IDFieldTemplate template = new IDFieldTemplate();
template.TextChangedHandler += tb1_TextChanged;

TemplateField tpfield = new TemplateField();
tpfield.HeaderText = "ID"
tpfield.ItemTemplate = template;
gv.Columns.Add(tpfield);


NB: If you need to support two-way data binding, your template class will also need to implement the IBindableTemplate interface[^].
 
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