dynamic Template DataGrid Problem

T

Tom Vukovich

Hi All,

I am having a bit of a problem retrieving the datagrid object to retrieve values the uses enters.

When i code the Columns in the HTML page everything works fine.

<asp:datagrid id="dgDispatch" Width="70%" Font-Size="x-small" runat="server">
<Columns>
<asp:BoundColumn HeaderText = "Hour Ending" DataField="Hour Ending" ></asp:BoundColumn>
<asp:BoundColumn HeaderText = "Previous" DataField="Previous" ></asp:BoundColumn>
<asp:BoundColumn HeaderText = "MW Request" DataField="MW Request" ></asp:BoundColumn>
<asp:BoundColumn HeaderText = "Hour Ending" DataField="Hour Ending1" ></asp:BoundColumn>
<asp:BoundColumn HeaderText = "Previous" DataField="Previous1" ></asp:BoundColumn>
<asp:BoundColumn HeaderText = "MW Request" DataField="MW Request1" ></asp:BoundColumn>
</Columns>

But I want the flexibility of building the columns in code. When the user click on the Edit button a template column is created and a usercontrol is assigned the value of the column to be edited.
'Dynamic column
Dim col5 As New TemplateColumn
col5.ItemTemplate = Page.LoadTemplate("ItemEdit1.ascx")
col5.HeaderStyle.Width = Unit.Percentage(20)
col5.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
col5.HeaderText = "Requested MW"
col5.ItemStyle.HorizontalAlign = HorizontalAlign.Center
dgDispatch.Columns.Add(col5)

the itemedit1.ascx code,

<table>
<tr>
<td>
<asp:RadioButtonList AutoPostBack="false" Font-Size=x-small RepeatDirection="Horizontal" ID="rbEdit" Runat="server">
<asp:ListItem Selected="true" Value="0">Max</asp:ListItem>
<asp:ListItem Selected="false" Value="1">Min</asp:ListItem>
<asp:ListItem Selected="false" Value="2">Other</asp:ListItem>
</asp:RadioButtonList>
</td>
<td align = right>
<asp:TextBox BackColor="#ffffcc" Font-Size=x-small Width = 60 ID=txtMW1 Runat=server Text= '<%# DataBinder.Eval(Ctype(Container,DataGridItem).dataitem, "MW Request1") %>'>
</asp:TextBox>
</td>
</tr>
</table>


When I generate the table contents in code, the datagrid object is not available following a post back. However when i place the datagrid templates in the page HTML all works fine.

There must be some little thing i am doing wrong. I hope someone can help.

tv
(e-mail address removed)
 
G

Guogang

Try to re-generate your datagrid in "Page_Load" function. Dynamic content
must be created here so that event can be wired correctly.

Read MSDN help "lifecycle for server controls" may be helpful.


Hi All,

I am having a bit of a problem retrieving the datagrid object to retrieve
values the uses enters.

When i code the Columns in the HTML page everything works fine.

<asp:datagrid id="dgDispatch" Width="70%" Font-Size="x-small"
runat="server">
</asp:BoundColumn>
<asp:BoundColumn HeaderText = "Previous" DataField="Previous"
</asp:BoundColumn>
<asp:BoundColumn HeaderText = "MW Request" DataField="MW Request"
</asp:BoundColumn>
<asp:BoundColumn HeaderText = "Hour Ending" DataField="Hour Ending1"
</asp:BoundColumn>
<asp:BoundColumn HeaderText = "Previous" DataField="Previous1"
</asp:BoundColumn>
<asp:BoundColumn HeaderText = "MW Request" DataField="MW Request1"
</asp:BoundColumn>
</Columns>

But I want the flexibility of building the columns in code. When the user
click on the Edit button a template column is created and a usercontrol is
assigned the value of the column to be edited.
'Dynamic column
Dim col5 As New TemplateColumn
col5.ItemTemplate = Page.LoadTemplate("ItemEdit1.ascx")
col5.HeaderStyle.Width = Unit.Percentage(20)
col5.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
col5.HeaderText = "Requested MW"
col5.ItemStyle.HorizontalAlign = HorizontalAlign.Center
dgDispatch.Columns.Add(col5)

the itemedit1.ascx code,

<table>
<tr>
<td>
<asp:RadioButtonList AutoPostBack="false" Font-Size=x-small
RepeatDirection="Horizontal" ID="rbEdit" Runat="server">
<asp:ListItem Selected="true" Value="0">Max</asp:ListItem>
<asp:ListItem Selected="false" Value="1">Min</asp:ListItem>
<asp:ListItem Selected="false" Value="2">Other</asp:ListItem>
</asp:RadioButtonList>
</td>
<td align = right>
<asp:TextBox BackColor="#ffffcc" Font-Size=x-small Width = 60 ID=txtMW1
Runat=server Text= '<%#
DataBinder.Eval(Ctype(Container,DataGridItem).dataitem, "MW Request1") %>'>
</asp:TextBox>
</td>
</tr>
</table>


When I generate the table contents in code, the datagrid object is not
available following a post back. However when i place the datagrid templates
in the page HTML all works fine.

There must be some little thing i am doing wrong. I hope someone can help.

tv
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top