Retrieve value for selected radiobutton in ListView asp.net -
i send guid should associated each radio button eventargs when user clicks checkout. able achieve functionality radiobuttonlist cannot use here rest of fields pulled database.
there numerous questions dealing type of topic not find 1 addresses trying achieve.
i have below list
<asp:content id="subscriptioncontent" contentplaceholderid="maincontent" viewstatemode="enabled" runat="server"> <asp:panel id="subscriptionpanel" cssclass="shopping-cart" viewstatemode="enabled" runat="server"> <asp:listview id="newsubscription" runat="server"> <layouttemplate> <table class="table"> <thead> <th>select subscription</th> <th>subscription level </th> <th>description </th> <th>price </th> </thead> <tbody> <tr id="itemplaceholder" runat="server" /> </tbody> </table> </layouttemplate> <itemtemplate> <tr> <td class="description"><asp:radiobutton id="subscriptionlevel" groupname="subscriptionradio" text='<%# eval("subscriptionlevel") %>' runat="server" /></td> <td class="description"><asp:label id="details" text='<%# eval("details") %>' runat="server"></asp:label></td> <td class="price"><asp:label runat="server" text='<%# eval("price", "{0:c2}") %>'></asp:label></td> <asp:textbox id="id" runat="server" visible="false" text='<%# eval("id") %>' /> </tr> </itemtemplate> </asp:listview> <asp:button id="subscribe" cssclass="btn btn-primary" runat="server" text="<%$ snippet: ecommerce/shoppingcart/checkoutbuttonlabel, checkout %>" onclick="buysubscription" /> <script type="text/javascript"> $(document).ready(function () { $("input:radio").attr('name', 'subscriptionradio');//override naming asp }); </script> </asp:panel> </asp:content>
i thinking if update hidden field corresponding guid value each radio button , submit when user triggers buysubscription
. not sure how though. want user able select 1 of subscription options , pass guid function.
thank in advance input.
the first problem you're going run asp.net gives each <input type="radio" />
different name, because it's in different namingcontainer
.
you've added script try work around changing name attribute on client. unfortunately, won't work. when post form server, asp.net still looking value using generated name, won't exist.
the quick , dirty solution update script copy value hidden textbox
value attribute of radiobutton. have use request.form["subscriptionradio"]
retrieve value of selected option:
<itemtemplate> <tr> <td class="description"> <asp:radiobutton id="subscriptionlevel" runat="server" groupname="subscriptionradio" text='<%# eval("subscriptionlevel") %>' /> <%-- nb: can't set visible="false", otherwise it's not rendered: --%> <asp:textbox id="id" runat="server" style="display:none;" text='<%# eval("id") %>' /> </td> ... </tr> </itemtemplate> ... <script> $(document).ready(function() { $("input:radio").each(function(){ var me = $(this); var id = me.closest("tr").find("input[name$=id]").val(); me.attr("value", id); me.attr('name', 'subscriptionradio'); }); }); </script>
alternatively, use custom radiobutton
control works within data-bound control. posted simple example in 2012: https://stackoverflow.com/a/13271444/124386
using system; using system.collections.specialized; using system.componentmodel; using system.reflection; using system.web.ui; using system.web.ui.webcontrols; namespace sitecontrols { [toolboxdata("<{0}:listradiobutton runat=\"server\" />")] public class listradiobutton : radiobutton { private static readonly fieldinfo uniquegroupnamefield = finduniquegroupnamefield(); private string _uniquegroupname; private static fieldinfo finduniquegroupnamefield() { return typeof(radiobutton).getfield("_uniquegroupname", bindingflags.nonpublic | bindingflags.instance); } public string value { { return attributes["value"]; } set { attributes["value"] = value; } } protected virtual string createuniquegroupname() { string result = groupname; if (string.isnullorempty(result)) { result = id; } if (string.isnullorempty(result)) { result = uniqueid; } else { control container = namingcontainer; if (container != null) { if (container idataitemcontainer) { container = container.namingcontainer ?? container; } result = container.uniqueid + base.idseparator + result; } else { string uniqueid = uniqueid; if (!string.isnullorempty(uniqueid)) { int index = uniqueid.lastindexof(base.idseparator); if (index != -1) { result = uniqueid.substring(0, 1 + index) + result; } } } } return result; } private void ensureuniquegroupname() { if (_uniquegroupname == null) { string value = createuniquegroupname(); if (uniquegroupnamefield != null) uniquegroupnamefield.setvalue(this, value); _uniquegroupname = value; value = base.attributes["value"]; if (string.isnullorempty(value)) { base.attributes["value"] = uniqueid; } } } protected override bool loadpostdata(string postdatakey, namevaluecollection postcollection) { ensureuniquegroupname(); return base.loadpostdata(postdatakey, postcollection); } protected override void render(htmltextwriter writer) { ensureuniquegroupname(); base.render(writer); } } }
you can either register control in page markup:
<%@ register tagprefix="site" namespace="sitecontrols" %>
or in web.config
file:
<?xml version="1.0"?> <configuration> <system.web> <pages> <controls> <add tagprefix="site" namespace="sitecontrols" /> </controls> </pages> </system.web> </configuration>
with in place, can lose script , hidden textbox
:
<itemtemplate> <tr> <td class="description"><site:listradiobutton id="subscriptionlevel" runat="server" groupname="subscriptionradio" text='<%# eval("subscriptionlevel") %>' value='<%# eval("id") %>' /></td> ...
to find selected item, need loop through listview
's items
, find radiobutton
control, , @ checked
property:
listradiobutton selecteditem = newsubscription.items .select(item => (listradiobutton)item.findcontrol("subscriptionlevel")) .firstordefault(radio => radio != null && radio.checked); string selectedvalue = (selecteditem == null) ? null : selecteditem.value;
Comments
Post a Comment