c# - ASP.NET Repeater: Button in item template to redirect to another page -
i have repeater , each item contains button should redirect page , pass value in query string.
i not getting errors, when click button, page refreshes (so assuming postback
does occur) , not redirect. think reason, isn't recognizing commandname
of button.
repeater code:
<asp:repeater id="mysponsoredchildrenlist" runat="server" onitemdatabound="mysponsoredchildrenlist_itemdatabound" onitemcommand="mysponsoredchildrenlist_itemcommand"> <headertemplate> </headertemplate> <itemtemplate> <br /> <div id="outerdiv"> <div id="innerleft"> <asp:image id="profilepic" runat="server" imageurl='<%#"~/resources/children images/" + string.format("{0}", eval("primary_image")) %>' width='300px' style="max-height: 500px" /></div> <div id="innerright"> <asp:hiddenfield id="childid" runat="server" value='<%# databinder.eval(container.dataitem, "child_id") %>'/> <span style="font-size: 20px; font-weight:bold;"><%# databinder.eval(container.dataitem, "name") %> <%# databinder.eval(container.dataitem, "surname") %></span> <br /><br /><br /> have been to? <br /><br /> <span style="font-style:italic">"<%# databinder.eval(container.dataitem, "mostrecentupdate")%>"</span> <span style="font-weight:bold"> -<%# databinder.eval(container.dataitem, "update_date", "{0:dd/mm/yyyy}")%></span><br /><br /><br />sponsored till: <%# databinder.eval(container.dataitem, "end_date", "{0:dd/mm/yyyy}")%> <br /><br /> <asp:button id="childprofilebutton" runat="server" text="view profile" commandname="viewprofile" /> </div> </div> <br /> </itemtemplate> <separatortemplate> <div id="separatordiv"> </div> </separatortemplate> </asp:repeater>
c# code behind:
protected void mysponsoredchildrenlist_itemdatabound(object sender, repeateritemeventargs e) { if (e.item.itemtype == listitemtype.alternatingitem || e.item.itemtype == listitemtype.item) { // stuff databind button mybutton = (button)e.item.findcontrol("childprofilebutton"); mybutton.commandname = "viewprofile"; } } protected void mysponsoredchildrenlist_itemcommand(object source, repeatercommandeventargs e) { if (e.commandname == "viewprofile") { int childidquery = convert.toint32(e.item.findcontrol("childid")); response.redirect("~/childdescription.aspx?id=" + childidquery); } }
i new using repeaters it's rookie mistake. on side note: there better way of obtaining childid without using hidden field?
edit: using breakpoints; itemdatabound event handler being hit, itemcommand not being entered @ all
you need set mysponsoredchildrenlist_itemdatabound protected. right now, have 'void' default private, , not accessible front aspx page.
another way use add event handler syntax function in code behind, using += operator.
either way, breakpoint hit , our code should mwork.
edit: above solved compilation error breakpoints not being hit; i've ran tests , able hit breakpoints this:
since not know how databinding, added code code-behind:
protected void page_load(object sender, eventargs e) { if (!ispostback) { mysponsoredchildrenlist.datasource = new list<object>() { null }; mysponsoredchildrenlist.databind(); } }
note: if databind() , itemdatabound() called on every postback, wipe out command argument, potentially seeeing; if see [object]_itemdatabound() breakpoint hit, never [object]_itemcommand(), because need databind on initial page load, or after major changes made.
note method mysponsoredchildrenlist_itemcommand doesn't work:
protected void mysponsoredchildrenlist_itemcommand(object source, repeatercommandeventargs e) { if (e.commandname == "viewprofile") { int childidquery = convert.toint32(e.item.findcontrol("childid")); response.redirect("~/childdescription.aspx?id=" + childidquery); } }
when findcontrol, need cast correct control type, , make sure check empty , null values before converting, or else possibly have errors:
protected void mysponsoredchildrenlist_itemcommand(object source, repeatercommandeventargs e) { if (e.commandname == "viewprofile") { int childid = 0; var hiddenfield = e.item.findcontrol("childid") hiddenfield; if (null != hiddenfield) { if (!string.isnullorempty(hiddenfield.value)) { childid = convert.toint32(hiddenfield.value); } } if (childid > 0) { response.redirect("~/childdescription.aspx?id=" + childid); } } }
hope helps - if not, please post additional code "full picture" of happening, can see else might have problem.
Comments
Post a Comment