dynamic Template DataGrid Problem

Discussion in 'ASP .Net Datagrid Control' started by Tom Vukovich, Jan 21, 2004.

  1. Tom Vukovich

    Tom Vukovich Guest

    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
     
    Tom Vukovich, Jan 21, 2004
    #1
    1. Advertising

  2. Tom Vukovich

    Guogang Guest

    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.


    "Tom Vukovich" <> wrote in message
    news:bumj98$nt0$...
    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
     
    Guogang, Jan 21, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    486
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    548
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,128
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    790
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    538
Loading...

Share This Page