asp.net - Gridview rowcommand event not firing in dynamically added usercontrol -


i have usercontrol gridview , rowcommand event. usercontrol added dynamically using loadcontrol on button click of page. gridview's rowcommand doesn't fire.

here code loads usercontrol on button click:

protected sub btnsearch_click(byval sender object, byval e eventargs) handles btnsearch.click      '<uctitle:searchlist id="ucsearchlist" runat="server" visible="false" />     dim ucsearchlist titlesearchlist = loadcontrol("~/controls/titlesearchlist.ascx")     ucsearchlist.isbn = txtsearchisbn.text     ucsearchlist.loadtitlesearchlist()     pnlsearchresults.controls.add(ucsearchlist)  end sub 

and here code in usercontrol

public class titlesearchlist inherits system.web.ui.usercontrol

public property isbn string  protected sub page_load(byval sender object, byval e system.eventargs) handles me.load     if not ispostback         loadtitlesearchlist()     end if end sub  public sub loadtitlesearchlist()      dim _isbn string = isbn      dim titles new list(of title)      titles = new list(of title) {                                         new title {.isbn = _isbn, .titlename = "title check"},                                         new title {.isbn = _isbn, .titlename = "title check"},                                         new title {.isbn = _isbn, .titlename = "title check"},                                         new title {.isbn = _isbn, .titlename = "title check"}                                     }     gvtitle.datasource = titles     gvtitle.databind()  end sub  public sub gvtitle_rowcommand(byval sender object, byval e gridviewcommandeventargs) handles gvtitle.rowcommand      if e.commandname = "titledetail"          response.redirect("titlesearch.aspx?isbn=" & e.commandargument().tostring())      end if  end sub end class 

events in gridviews added dynamically in usercontrol little ugly. since usercontrols added dynamically have re-added on post-back, have rebind gridview datasource, makes lose out on automatically getting events fired you. being said, can still pull off parsing of __eventtarget of form.

add hiddenfield tells whether or not need re-add usercontrol. add in page_load event handler:

    if cbool(hdntitlesearchactive.value) = true          addsearchlisttopanel()      end if 

call addsearchlisttopanel() in btnsearch_click event handler.

now implementation of addsearchlisttopanel can cleaned some, should enough going. note button triggering gridview command in example has id of lbttest. you have adjust based on id using.

private sub addsearchlisttopanel()      dim ucsearchlist titlesearchlist = loadcontrol("~/controls/titlesearchlist.ascx")     ucsearchlist.isbn = txtsearchisbn.text     ucsearchlist.loadtitlesearchlist()     pnlsearchresults.controls.add(ucsearchlist)      hdntitlesearchactive.value = true      dim streventtarget string = httpcontext.current.request.form("__eventtarget")      if not streventtarget nothing andalso streventtarget.contains("gvtitle$") andalso _            streventtarget.contains("$lbttest")          'value example = gvtitle$ctl02$lbttest         dim intrownumber integer = (cint(streventtarget.substring(11, 2)) - 1)          dim lbtcommandsource linkbutton = ctype(ctype(ucsearchlist.findcontrol("gvtitle"), gridview).rows(intrownumber).findcontrol("lbttest"), linkbutton)          dim objcommandeventarguments new commandeventargs(lbtcommandsource.commandname, lbtcommandsource.commandargument)          dim objgridviewcommandeventargs new gridviewcommandeventargs(lbtcommandsource, objcommandeventarguments)          ucsearchlist.gvtitle_rowcommand(lbtcommandsource, objgridviewcommandeventargs)      end if  end sub 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -