Wednesday, 15 April 2015

asp.net - using ClientScriptIncludeRegister to get a hidden field value -



asp.net - using ClientScriptIncludeRegister to get a hidden field value -

i implementing web application using asp.net/vb. front-end (.aspx) executes external .js file as:

<script type =" text/javascript" src="external.js"></script>

where contains functions. 1 of these functions called populatehidden() used assign value hiddenfield defined on front-end (.aspx) follows:

in external.js

document.getelementbyid('hidden2').value = "dsadsadas";

in .aspx

<input id="hidden2" type ="hidden" runat="server" />

what have been trying value assigned hidden2 , pass server-side (.aspx.vb) using:

dim str = hidden2.value

however, since server-side code executes first,str empty , unless postback done somehow whether using button or timer reload front-end, str have dsadsadas. not intend reload page or initialize postback. tried window.onload = populatehidden() no luck. situation made me desperate since tried many things making sure not utilize postbacks or reloads until came across clientscriptmanager.registerclientscriptinclude method . couldn't not around onto how can utilize such illustration solve situation.

the thought in mind phone call or execute external.js server side (since executes first), populate hidden2 on front-end, go server side , retrieve hidden2.value.

however illustration in link mentioned earlier, server-side code written in front-end want write on server-side (.aspx.vb).

the reason why need hidden2.value in server-side store in sql_database. suggestions, advice or solutions hidden2.value front-end appreciated.

the next solution uses asp.net ajax engine. in pageload event, phone call populatehidden() function beingness registered. in codebehind, method marked webmethod attribute added, allowing called ajax request (without postback). so, when button clicked, javascript function sendhiddenvaluetoserver() called, making ajax request page method, passing hidden field value parameter.

first, need scriptmanager declared enablepagemethods property set true:

<asp:scriptmanager runat="server" enablepagemethods="true" />

i tested using next markup:

<html> <head runat="server"> <title></title> <script src="external.js" type="text/javascript"></script> </head> <body> <form id="form1" runat="server"> <asp:scriptmanager runat="server" enablepagemethods="true" /> <div> <asp:hiddenfield id="hidden2" runat="server" clientidmode="static" /> <button id="button1" onclick="sendhiddenvaluetoserver();"> send value server</button> </div> </form> </body> </html>

in javascript file:

function populatehidden() { document.getelementbyid('hidden2').value = "dsadsadas"; } function sendhiddenvaluetoserver() { pagemethods.receivehiddenvalue( document.getelementbyid('hidden2').value, function () { alert("success!") }, function () { alert("error!") }); }

and in codebehind:

protected sub page_load(byval sender object, byval e system.eventargs) handles me.load if not page.ispostback scriptmanager.registerstartupscript(me, me.gettype(), "register", "populatehidden();", true) end if end sub <system.web.services.webmethod()> public shared sub receivehiddenvalue(byval value string) dim str string = value ' save value database end sub

asp.net vb.net visual-studio-2010 .net-4.0

No comments:

Post a Comment