Initialize dynamic controls on postback

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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="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("Pr oductOptions");


    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[i]["ProductSKU"].ToString();
    rad.EnableViewState = true;
    Label lblFinish = new Label();
    lblFinish.Text = dvOptions[i]["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[i]["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 Guest

  2. Similar Questions and Discussions

    1. Controls Postback --- Please help urgent
      I have two controls Control A has a text box and button Control B has a text box Both these controls are pasted on custompage.aspx. When I...
    2. User Controls Postback
      I have two user controls Control1 and Control2 (.ascx). When the Web Page containing this control gets posted back I have some logic to determine...
    3. postback and nonpostback controls
      hi all iam in a confusion of which of the controls are postback controls and which of them are nonpostback controls and what is the reason for the...
    4. Child Controls => Postback? => null?
      Hi there were a few postings earlier, including mine regarding this. It looks as if when a dynamic child control causes a postback, its handler...
    5. Validation Controls Postback
      I have a little problem while working with Validation Controls in extensive forms... When the user reaches the end, and something is wrong, it just...
  3. #2

    Default 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@waveshift.com> wrote in message
    news:eu7N2%23qVDHA.2352@TK2MSFTNGP12.phx.gbl...
    > 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("Pr oductOptions");
    >
    >
    > 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[i]["ProductSKU"].ToString();
    > rad.EnableViewState = true;
    > Label lblFinish = new Label();
    > lblFinish.Text = dvOptions[i]["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[i]["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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139