Initialize dynamic controls on postback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Miller

    #1

    Initialize dynamic controls on postback

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

    ..aspx file:
    <asp:datagrid id="dgProductOp tions" runat="server"
    AutoGenerateCol umns="False" OnItemDataBound ="LoadChildReco rds" Width="100%">


    codebehind:
    public void LoadChildRecord s(object Sender, DataGridItemEve ntArgs e){
    if(e.Item.ItemT ype == ListItemType.Al ternatingItem || e.Item.ItemType ==
    ListItemType.It em){
    DataList dlPrintFinish = (DataList)e.Ite m.FindControl(" dlPrintFinish") ;
    DataView dvOptions =
    ((DataRowView)e .Item.DataItem) .CreateChildVie w("ProductOptio ns");


    System.Web.UI.H tmlControls.Htm lTable tbl = new HtmlTable();
    tbl.Border = 0;
    tbl.CellPadding = 0;
    tbl.CellSpacing = 0;
    tbl.Width = "100%";
    for(int i=0; i<dvOptions.Cou nt; i++){
    HtmlTableRow tr = new HtmlTableRow();
    HtmlTableCell td = new HtmlTableCell() ;
    HtmlInputRadioB utton rad = new HtmlInputRadioB utton();
    rad.ID = "radFinish" ;
    rad.Name = "radFinish" ;
    rad.Value = dvOptions[i]["ProductSKU "].ToString();
    rad.EnableViewS tate = true;
    Label lblFinish = new Label();
    lblFinish.Text = dvOptions[i]["FinishDescript ion"].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(dv Options[i]["MSRP"].ToString()).To String("C");
    td2.Controls.Ad d(lbl);
    tr.Cells.Add(td 2);
    tbl.Rows.Add(tr );
    }
    phdPrintFinish = (PlaceHolder)e. Item.FindContro l("phdPrintFini sh");
    phdPrintFinish. Controls.Add(tb l);
    }
    }


    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

    #2
    Re: Initialize dynamic controls on postback

    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" <no_mark_spam@w aveshift.com> wrote in message
    news:eu7N2%23qV DHA.2352@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > I have created a number of RadioButton controls during the OnItemDataBound
    > event of a datagrid control.
    >
    > .aspx file:
    > <asp:datagrid id="dgProductOp tions" runat="server"
    > AutoGenerateCol umns="False" OnItemDataBound ="LoadChildReco rds"[/color]
    Width="100%">[color=blue]
    >
    >
    > codebehind:
    > public void LoadChildRecord s(object Sender, DataGridItemEve ntArgs e){
    > if(e.Item.ItemT ype == ListItemType.Al ternatingItem || e.Item.ItemType ==
    > ListItemType.It em){
    > DataList dlPrintFinish = (DataList)e.Ite m.FindControl(" dlPrintFinish") ;
    > DataView dvOptions =
    > ((DataRowView)e .Item.DataItem) .CreateChildVie w("ProductOptio ns");
    >
    >
    > System.Web.UI.H tmlControls.Htm lTable tbl = new HtmlTable();
    > tbl.Border = 0;
    > tbl.CellPadding = 0;
    > tbl.CellSpacing = 0;
    > tbl.Width = "100%";
    > for(int i=0; i<dvOptions.Cou nt; i++){
    > HtmlTableRow tr = new HtmlTableRow();
    > HtmlTableCell td = new HtmlTableCell() ;
    > HtmlInputRadioB utton rad = new HtmlInputRadioB utton();
    > rad.ID = "radFinish" ;
    > rad.Name = "radFinish" ;
    > rad.Value = dvOptions[i]["ProductSKU "].ToString();
    > rad.EnableViewS tate = true;
    > Label lblFinish = new Label();
    > lblFinish.Text = dvOptions[i]["FinishDescript ion"].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(dv Options[i]["MSRP"].ToString()).To String("C");
    > td2.Controls.Ad d(lbl);
    > tr.Cells.Add(td 2);
    > tbl.Rows.Add(tr );
    > }
    > phdPrintFinish = (PlaceHolder)e. Item.FindContro l("phdPrintFini sh");
    > phdPrintFinish. Controls.Add(tb l);
    > }
    > }
    >
    >
    > 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[/color]
    can[color=blue]
    > I recreate these controls on postback in order to read their values?
    >
    >
    >[/color]


    Comment

    Working...