c# - GridView - Dynamic Button Control Click Event Not Firing - Sometimes -


i have 2 gridviews on single page, each gridview modified in code-behind group using rowdatabound , within rowdatabound adding textbox , button each grouping. button added click event. also, each gridview in updatepanel.

in order around postback issue rowdatabound items , dynamic controls have added simple response.redirect same page. code works buttons stop firing click event half way down gridview list. after half way mark click acts initiating postback instead of firing click event, gives same results problem of rowdatabound information being stripped out.

from can tell, click event not being called. code have.

protected void gridviewrowdatabound(object sender, gridviewroweventargs e) {         nonpaidclb.visible = true;         if (e.row.rowtype == datacontrolrowtype.datarow)         {             datarowview drv = (datarowview)e.row.dataitem;             if (tmpcategoryname != drv["num"].tostring())             {                 string usetable = "";                 string giftdoorprize = "";                 if (drv["table"].equals("n"))                 {                     usetable = " - not using table";                 }                 if (drv["gift"].equals("y"))                 {                     giftdoorprize = " - providing gift or door prize";                 }                 tmpcategoryname = drv["num"].tostring();                 table tbl = e.row.parent table;                 if (tbl != null)                 {                     gridviewrow row = new gridviewrow(-1, -1, datacontrolrowtype.datarow, datacontrolrowstate.normal);                     tablecell cell = new tablecell();                     cell.columnspan = this.gridview.columns.count;                     cell.style.add("font-weight", "bold");                     cell.style.add("background-color", "#4382c0");                     cell.style.add("color", "white");                      textbox notes2txtbox = new textbox();                     notes2txtbox.id = "notes2txtbox";                     notes2txtbox.attributes.add("runat", "server");                     notes2txtbox.text = drv["notes"].tostring();                     notes2txtbox.style.add("width", "400px");                      label payinfoid2txtbox = new label();                     payinfoid2txtbox.id = "payinfoid2txtbox";                     payinfoid2txtbox.text = drv["payid"].tostring();                     payinfoid2txtbox.attributes.add("runat", "server");                      button savenotes2btn = new button();                     savenotes2btn.id = "savenotes2btn";                     savenotes2btn.text = "update";                     savenotes2btn.attributes.add("runat", "server");                     savenotes2btn.click += savenotes2_onclick;                      string paidstr = string.empty;                     string invoicereceiptstr = string.empty;                     decimal totalcost = 0;                     totalcost = (convert.toint16(drv["numatt"]) - 1) * convert.todecimal(drv["fullf"]) + convert.todecimal(drv["partf"]) + convert.todecimal(drv["cspf"]);                      if (drv["pd"].equals("y"))                     {                         paidstr = "<strong>paid</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";                         invoicereceiptstr = " <a href='resendreceipt.aspx?payregid=" + drv["paymentinfoid"] + "&totalcost=" + totalcost + "' style='color:white;' >resend receipt</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";                     }                     else                     {                         paidstr = "<a href='confirmpaid.aspx?payregid=" + drv["payid"] + "' style='color:black;' >mark paid</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";                         invoicereceiptstr = " <a href='resendinvoice.aspx?payregid=" + drv["payid"] + "&totalcost=" + totalcost + "' style='color:white;' >resend invoice</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";                     }                     string csptext = "";                     if (!drv["cspf"].tostring().equals("0.0000"))                     {                         csptext = ", cspresenter ($" + convert.todecimal(drv["concursespresfee"]).tostring("#,##0.00") + ")";                     }                     htmlgenericcontrol span = new htmlgenericcontrol("span");                     span.innerhtml = "<a href='/events/invoice.aspx?confid=" + conferenceddl.selectedvalue + "&payid=" + drv["payid"] + "' target='_blank' style='color:white;' >" + tmpcategoryname + "</a>" + usetable + "" + giftdoorprize + ",&nbsp;sponsor fee: $" + convert.todecimal(drv["partf"].tostring()).tostring("#,##0.00") + csptext + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + paidstr + invoicereceiptstr + "<div style='float:right;'>total: $" + convert.todecimal(totalcost).tostring("#,##0.00") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;notes:&nbsp;";                     cell.controls.add(span);                     cell.controls.add(notes2txtbox);                     cell.controls.add(savenotes2btn);                     cell.controls.add(new literalcontrol("</div>"));                     row.cells.add(cell);                     tbl.rows.addat(tbl.rows.count - 1, row);                 }             }         }     } } 

like said, code works first several items in gridview stops working. help?

you cannot add controls dynamically, , catch event way doing.

step 1

you need place servercontrols inside gridview's template columns.

step 2

find servercontrol want manipulate inside gridviewrowdatabound. example,

button button = (button)e.row.findcontrol("mybutton"); 

then whatever want servercontrol based on logic.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -