Thursday, 15 March 2012

asp.net - Do a Button Click from Code behide -



asp.net - Do a Button Click from Code behide -

i have gridview lists tools , access values. edit have edit imagebutton on each row. have onrowbound method assigns onclick attribute each button know record need edit. code is

protected sub changefirstrowicon(byval sender object, byval e gridviewroweventargs) handles gv_accessrights.rowdatabound 'this sub fires on each gridview row created... 'it first checks row info row (as opposed header, footer etc.) 'if ib_edit true alter add together attribut button aid, tid , ac values attached. if e.row.rowtype = datacontrolrowtype.datarow dim ib_edit imagebutton = e.row.findcontrol("ib_edit") dim lb_accessid label = e.row.findcontrol("lb_accessid") dim hd_toolid hiddenfield = e.row.findcontrol("hd_toolid") dim hd_accesscode hiddenfield = e.row.findcontrol("hd_accesscode") if ib_edit isnot nil ib_edit.attributes.add("onclick", "proxyclick('" & lb_accessid.text & "', '" & hd_toolid.value & "', '" & hd_accesscode.value & "')") end if end if end sub

i'm using hidden proxy button show modal popup user utilize edit record... (the same popup used add together new access record... come later). having passed details proxyclick set values controls within modal popup. javascript is....

<script type="text/javascript"> function proxyclick(aid, tid, ac) { document.getelementbyid('hd_accessid').value = aid; document.getelementbyid('hd_toolidmod').value = tid; document.getelementbyid('hd_accesscodemod').value = ac; document.getelementbyid('but_setmodaldetails').click(); } </script>

for reference main bits of markup are....

<table class="border"> <tr> <td> <asp:button id="but_setmodaldetails" runat="server" style="display: none" text="set modal details" clientidmode="static" usesubmitbehavior="true" /> <asp:button id="but_hiddenproxy" runat="server" style="display: none" text="hidden proxy button modal popup" clientidmode="static" /> </td> <td class="rt"> <asp:button id="but_addtool" runat="server" accesskey="a" cssclass="butgreen" text="add tool" tooltip="add tool - alt a" /> </td> </tr> </table> <asp:modalpopupextender id="mpx_addeditaccess" runat="server" cancelcontrolid="but_cancel" behaviorid="pn_addeditaccess" popupcontrolid="pn_addeditaccess" targetcontrolid="but_hiddenproxy" backgroundcssclass="modalbackground" /> <asp:panel id="pn_addeditaccess" runat="server" width="500px" cssclass="modalpopup" style="display: block"> <div class="box"> <h2> <asp:label id="lb_modtitle" runat="server"></asp:label> </h2> <asp:hiddenfield id="hd_accessid" runat="server" clientidmode="static"></asp:hiddenfield> <div class="block"> <asp:updatepanel id="up_access" runat="server" updatemode="always"> <triggers> <asp:asyncpostbacktrigger controlid="ddl_toolname" eventname="selectedindexchanged" /> </triggers> <contenttemplate> <table> <tr> <th class="p66 rt"> tool name:&nbsp; </th> <td class="p66"> <asp:hiddenfield id="hd_toolidmod" runat="server" clientidmode="static" /> <asp:dropdownlist id="ddl_toolname" runat="server" autopostback="true" appenddatabounditems="true" datasourceid="sqlds_tools" datatextfield="toolname" datavaluefield="toolid" onselectedindexchanged="ddl_toolname_sic"> <asp:listitem text="please select..." value="0"></asp:listitem> </asp:dropdownlist> <asp:sqldatasource id="sqlds_tools" runat="server" connectionstring="<%$ connectionstrings:toolsconnstring %>" selectcommand="select [toolid], [toolname] [tbl_tools] ([redundant] = @redundant)"> <selectparameters> <asp:parameter defaultvalue="false" name="redundant" type="boolean" /> </selectparameters> </asp:sqldatasource> <asp:requiredfieldvalidator id="rfv_ddl_toolname" runat="server" controltovalidate="ddl_toolname" cssclass="error" display="dynamic" errormessage="please select tool name" initialvalue="0"> </asp:requiredfieldvalidator> </td> </tr> <tr> <th class="p66 rt"> access rights:&nbsp; </th> <td class="p66"> <asp:hiddenfield id="hd_accesscodemod" runat="server" clientidmode="static" /> <asp:dropdownlist id="ddl_accesscode" runat="server" enabled="false"> <asp:listitem text="no access" value="0"></asp:listitem> </asp:dropdownlist> </td> </tr> <tr> <td class="p66"> <asp:button id="but_cancel" runat="server" text="cancel" /> </td> <td class="p66 rt"> <asp:button id="but_save" runat="server" text="save" /> </td> </tr> </table> </contenttemplate> </asp:updatepanel> </div> </div> </asp:panel>

as can see have implemented 2 hidden buttons but_setmodaldetails , but_hiddenproxy. but_setmodaldetails has codebehind sets couple of dropdown lists (one populated datasource, other populated dynamically based on value of first. codebehind is...

protected sub but_setmodaldetails_click(byval sender object, byval e eventargs) handles but_setmodaldetails.click if hd_accessid.value = "0" lb_modtitle.text = "assigning access rights to:" ddl_toolname.selectedindex = 0 ddl_accesscode.selectedindex = 0 ddl_accesscode.enabled = false else lb_modtitle.text = "edit access rights to:" ddl_toolname.selectedvalue = hd_toolidmod.value ddl_toolname.enabled = false sqlstr = "select accessid ddlvalue, accesstext ddltext tbl_accesscodes toolid = " & hd_toolidmod.value populateddlvalue(ddl_accesscode, sqlstr) ddl_accesscode.selectedvalue = hd_accesscodemod.value ddl_accesscode.enabled = true end if 'now need simulate but_hiddenproxy click end sub

as can see @ end need simulate click of but_hiddenproxy modalpopup shown populated right data.

any ideas? thanks

after that... able in codebehind...

i needed 1 hidden button but_hiddenproxy.

in gridview instead of setting onclick attribute each edit image button set commandname of 'accessedit' (don't utilize 'edit'). had method handled gridview.rowcommand event. found various info needed using findcontrol on selected row. these values used populate dropdowns in popup , utilize show command create popup visible.

one bit did stump me while why rowcommand not triggering when imagebutton clicked. i'd forgotten had validation in modal stopped rowcommand beingness executed. stuck causesvalidation="false" in imagebutton , ok.

talk using hammer crack nut!

asp.net button modalpopupextender

No comments:

Post a Comment