Initialize dynamic controls on postback

Discussion in 'ASP .Net' started by Mark Miller, Jul 30, 2003.

  1. Mark Miller

    Mark Miller Guest

    I have created a number of RadioButton controls during the OnItemDataBound
    event of a datagrid control.

    ..aspx file:
    <asp:datagrid id="dgProductOptions" runat="server"
    AutoGenerateColumns="False" OnItemDataBound="LoadChildRecords" Width="100%">


    codebehind:
    public void LoadChildRecords(object Sender, DataGridItemEventArgs e){
    if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
    ListItemType.Item){
    DataList dlPrintFinish = (DataList)e.Item.FindControl("dlPrintFinish");
    DataView dvOptions =
    ((DataRowView)e.Item.DataItem).CreateChildView("ProductOptions");


    System.Web.UI.HtmlControls.HtmlTable tbl = new HtmlTable();
    tbl.Border = 0;
    tbl.CellPadding = 0;
    tbl.CellSpacing = 0;
    tbl.Width = "100%";
    for(int i=0; i<dvOptions.Count; i++){
    HtmlTableRow tr = new HtmlTableRow();
    HtmlTableCell td = new HtmlTableCell();
    HtmlInputRadioButton rad = new HtmlInputRadioButton();
    rad.ID = "radFinish";
    rad.Name = "radFinish";
    rad.Value = dvOptions["ProductSKU"].ToString();
    rad.EnableViewState = true;
    Label lblFinish = new Label();
    lblFinish.Text = dvOptions["FinishDescription"].ToString();
    if(i==0){
    rad.Checked = true;
    }
    td.Controls.Add(rad);
    td.Controls.Add(lblFinish);
    tr.Cells.Add(td);
    HtmlTableCell td2 = new HtmlTableCell();
    Label lbl = new Label();
    lbl.Text = Double.Parse(dvOptions["MSRP"].ToString()).ToString("C");
    td2.Controls.Add(lbl);
    tr.Cells.Add(td2);
    tbl.Rows.Add(tr);
    }
    phdPrintFinish = (PlaceHolder)e.Item.FindControl("phdPrintFinish");
    phdPrintFinish.Controls.Add(tbl);
    }
    }


    I realize that I need to recreate these controls during the OnLoad event,
    but I'm not clear how to do this. The number of rows and the number of
    RadioButton controls per row are both unknown until I have the data. How can
    I recreate these controls on postback in order to read their values?
     
    Mark Miller, Jul 30, 2003
    #1
    1. Advertising

  2. Mark Miller

    MS News Guest

    That is a problem
    I had to re-create my controls by getting my data again from the database or
    from a persisted DataSet
    I hape you find a better answer


    "Mark Miller" <> wrote in message
    news:eu7N2%...
    > I have created a number of RadioButton controls during the OnItemDataBound
    > event of a datagrid control.
    >
    > .aspx file:
    > <asp:datagrid id="dgProductOptions" runat="server"
    > AutoGenerateColumns="False" OnItemDataBound="LoadChildRecords"

    Width="100%">
    >
    >
    > codebehind:
    > public void LoadChildRecords(object Sender, DataGridItemEventArgs e){
    > if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
    > ListItemType.Item){
    > DataList dlPrintFinish = (DataList)e.Item.FindControl("dlPrintFinish");
    > DataView dvOptions =
    > ((DataRowView)e.Item.DataItem).CreateChildView("ProductOptions");
    >
    >
    > System.Web.UI.HtmlControls.HtmlTable tbl = new HtmlTable();
    > tbl.Border = 0;
    > tbl.CellPadding = 0;
    > tbl.CellSpacing = 0;
    > tbl.Width = "100%";
    > for(int i=0; i<dvOptions.Count; i++){
    > HtmlTableRow tr = new HtmlTableRow();
    > HtmlTableCell td = new HtmlTableCell();
    > HtmlInputRadioButton rad = new HtmlInputRadioButton();
    > rad.ID = "radFinish";
    > rad.Name = "radFinish";
    > rad.Value = dvOptions["ProductSKU"].ToString();
    > rad.EnableViewState = true;
    > Label lblFinish = new Label();
    > lblFinish.Text = dvOptions["FinishDescription"].ToString();
    > if(i==0){
    > rad.Checked = true;
    > }
    > td.Controls.Add(rad);
    > td.Controls.Add(lblFinish);
    > tr.Cells.Add(td);
    > HtmlTableCell td2 = new HtmlTableCell();
    > Label lbl = new Label();
    > lbl.Text = Double.Parse(dvOptions["MSRP"].ToString()).ToString("C");
    > td2.Controls.Add(lbl);
    > tr.Cells.Add(td2);
    > tbl.Rows.Add(tr);
    > }
    > phdPrintFinish = (PlaceHolder)e.Item.FindControl("phdPrintFinish");
    > phdPrintFinish.Controls.Add(tbl);
    > }
    > }
    >
    >
    > I realize that I need to recreate these controls during the OnLoad event,
    > but I'm not clear how to do this. The number of rows and the number of
    > RadioButton controls per row are both unknown until I have the data. How

    can
    > I recreate these controls on postback in order to read their values?
    >
    >
    >
     
    MS News, Jul 30, 2003
    #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. Steve Roszko

    Dynamic Controls and Postback

    Steve Roszko, Oct 21, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    5,874
  2. Psych971
    Replies:
    5
    Views:
    4,636
    bruce barker
    Dec 17, 2004
  3. Ronald
    Replies:
    2
    Views:
    853
    Robbe Morris [C# MVP]
    Jan 17, 2006
  4. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    473
    Eidolon
    May 4, 2004
  5. Wael_Bakr

    stop postback in postback events for server controls ??

    Wael_Bakr, Nov 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    509
    Wael_Bakr
    Nov 30, 2005
Loading...

Share This Page