this class also has a public property to expose the dictionary object, which I can access from the parent page.
But this is where I seem to be struggling. I cant seem to get any OnCheckChanged events, to wire up to the checkboxes so I can update the dictionary object. Im assuming that is because the listview items are beeing generated at an inconvenient stage in the
lifecycle. So theevents are not wiring up correctly. I would rather not use events at all and just submit it all at once, but Im not understanding how to pass the collection of values back from the user control on submit of the parent page. If I am going
about this the wrong way, please enlighten me as to a better way to do this.
Im not understanding how to pass the collection of values back from the user control on submit of the parent page.
Hello:)
Because you've enclosed the user control and drag and drop it onto a normal aspx page,When you do submit the page,I think it will raise the event of Page_Load of the user control itself。So please handle that event。But you should save the Dictionary<K,V>
into a Session or ViewState so as to catch it in the event……
Zen_Dragon
Member
2 Points
7 Posts
Submitting values from dynamically generated fields, from a user control.
Jan 24, 2012 01:38 PM|LINK
I have a user control that uses a ListView to generate a list with checkboxes from a database
<asp:ListView ID="lTPAList" runat="server" > <LayoutTemplate> <table id="tpaCollectDirect" runat="server"> <thead> <tr> <th> TPAs </th> <th> <span>Collect Direct </span> </th> </tr> </thead> <tr id="ItemPlaceholder" runat="server" /> </table> </LayoutTemplate> <ItemTemplate> <tbody> <tr> <td class="list"> <%# Eval("name") %> </td> <td class="checkbox"> <asp:CheckBox ID="CollectDirect" runat="server" Checked='<%# Convert.ToBoolean(Eval("CollectDirect")) %>' /> <asp:HiddenField ID="AgoraId" runat="server" Value='<%# Eval("Agora_SourceID") %>' /> </td> </tr> </tbody> </ItemTemplate> <EmptyDataTemplate> No TPAs Defined </EmptyDataTemplate> </asp:ListView>Fairly straightforward... binding in the code behind:
lTPAList.ItemDataBound += new EventHandler<ListViewItemEventArgs>(lTPAList_ItemDataBound); lTPAList.DataSource = FundsDataHelper.GetTPAsByFundId(FundComplexId); lTPAList.DataBind();And the ItemDataBound event, which populates a dictionary<int, bool> with the values from the stored proc:
void lTPAList_ItemDataBound(object sender, ListViewItemEventArgs e) { ListViewDataItem item = (ListViewDataItem)e.Item; if (item.ItemType == ListViewItemType.DataItem) { HiddenField agorafield = (System.Web.UI.WebControls.HiddenField)item.FindControl("AgoraId"); CheckBox collectbox = (System.Web.UI.WebControls.CheckBox)item.FindControl("CollectDirect"); int agoraid = Convert.ToInt32(agorafield.Value); bool collectdirect = collectbox.Checked; _collectdirect.Add(agoraid, collectdirect); } }this class also has a public property to expose the dictionary object, which I can access from the parent page.
But this is where I seem to be struggling. I cant seem to get any OnCheckChanged events, to wire up to the checkboxes so I can update the dictionary object. Im assuming that is because the listview items are beeing generated at an inconvenient stage in the lifecycle. So theevents are not wiring up correctly. I would rather not use events at all and just submit it all at once, but Im not understanding how to pass the collection of values back from the user control on submit of the parent page. If I am going about this the wrong way, please enlighten me as to a better way to do this.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Submitting values from dynamically generated fields, from a user control.
Jan 26, 2012 10:57 AM|LINK
Hello:)
Because you've enclosed the user control and drag and drop it onto a normal aspx page,When you do submit the page,I think it will raise the event of Page_Load of the user control itself。So please handle that event。But you should save the Dictionary<K,V> into a Session or ViewState so as to catch it in the event……