i have hidden field run at server and i set value to it in my c# code and when i click a button i want that value transfer to my javascript or jquery , and also that button is in the update panel. so no postback occurs. i called it by registerstartupscript but didnt work , here is my c# code

using (MemoryStream ms = new MemoryStream())
{
     // Convert Image to byte[]
     image.Save(ms, ImageFormat.Png);
     byte[] imageBytes = ms.ToArray();

     // Convert byte[] to Base64 String
     base64String = "data:image/png;base64," + Convert.ToBase64String(imageBytes);
}

hidImgQR.Value = base64String;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "myScript", getImgQR();", true);

in jQUERY

function getImgQR() 
{   
    alert($("input[id$=hidImgQR]").val());      
}   

i have tried many different way but it doesnt work, may be because of update panel it doesnt take latest value but only the defalut value i have assigned ..

有帮助吗?

解决方案 2

Have Hidden field control

<asp:HiddenField id="fileId" runat="server" value='Nishant' />

JavaScript Code which will return hidden field Value

document.getElementById("<%= fileId.ClientID %>").value;

Note You can also make ClientIDMode="Static" use this property for your control and retrieve it by id itself 'fileId' ForInstance:

<asp:HiddenField id="fileId" runat="server" ClientIDMode="Static" value='Nishant' />

and use JavaScript code:

document.getElementById("fileId").value;

It will also return Nishant but Id will be static. Use only ClientIDMode="Static when you are sure that no one control has same Id.

Runnable Asp.Net Code http://runnable.com/UjsLAmP-yM0VAADp/asp-net-how-to-get-server-control-value-in-javascript

其他提示

You can also do something like this

$('#' + '<%= hidImgQR.ClientID %>').val();

You can do :

   alert($("<%= hidImgQR.ClientID %>").val());

call button's click inside pageLoad():

<script type="text/javascript"> 
function pageLoad() { 
    $("#yourbtn").click(function(){
    alert($("<%= hidImgQR.ClientID %>").val());
   });
  } 
</script>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top