Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi coders,

Here i am giving the small example about my requirement

I am using one java script function like

My aspx page Code:
JavaScript
<script type="text/Jscript"> 
function getvalue() {
    
                document.getElementById("hdnResultValue").value = 123;
            }
</script>

ASP.NET
<asp:HiddenField ID="hdnResultValue" Value="0" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>

My aspx page cs Code:
C#
protected void Button1_Click(object sender, EventArgs e)
        {
Page.ClientScript.RegisterStartupScript(this.GetType(),null, "getvalue();", true);
 string codeBehindValue = hdnResultValue.Value;
}


Here when I hit my button first time I am not getting the hdnResultValue value(Here I am getting 0), If I hit second time I am getting the hdnResultValue value(here I am getting 123)

This might be happening due to post back.

my problem is how can I can get the hdnResultValue value when hit my button first time.
Posted
Updated 9-Oct-13 0:50am
v2
Comments
Thanks7872 9-Oct-13 6:30am    
Why you called getvalue()? If you want to have 123 then why used 0?
D-Kishore 9-Oct-13 6:35am    
Hi, here I have given just example.
In my initial requirement I am doing some calculations and storing that value to hidden filed.
Thanks7872 9-Oct-13 6:37am    
Still not clear. Why you used JavaSCript here? Why not to simply do all that stuff inside Button1_Click? Use reply button while making comment.
D-Kishore 9-Oct-13 6:44am    
Ok, I will explain.

Here I am converting my gridview to image.

For this I am using
<script type="text/javascript" src="Styles/html2canvas.js"></script>
<script type="text/Jscript">
function getvalue() {
var canvas1 = document.getElementById("myCanvas");
html2canvas(document.getElementById("kk"), {


onrendered: function (canvas1) {
document.body.appendChild(canvas1);

var image = canvas1.toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
document.getElementById("hdnResultValue").value = image;
}
});
}
</script>

using the hidden field data in my button click event I am exporting as a image.
string codeBehindValue = hdnResultValue.Value;
string fileNameWitPath = path + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";

using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{

using (BinaryWriter bw = new BinaryWriter(fs))
{

byte[] data = Convert.FromBase64String(codeBehindValue);

bw.Write(data);

bw.Close();

}

}
D-Kishore 9-Oct-13 6:49am    
These java script function I am unable to wrote in button click event at codebehind.
because of this I am wrote the function in aspx page then I am calling the function in button click event using Page.ClientScript.RegisterStartupScript

Asp:Button's onclick is binding to server side handler.
If you want to run some js you need a client side handler. Something like this...

ASP.NET
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="getvalue();" />
 
Share this answer
 
v2
Comments
D-Kishore 9-Oct-13 6:37am    
But I need to fire this function in button click event only.
Kindly suggest any other possibilities.
Kornfeld Eliyahu Peter 10-Oct-13 7:16am    
You ARE firing on button click. The ASP.NET rendering will render client-side onclick event based on your OnClientClick declaration...

You may read here http://msdn.microsoft.com/en-us/library/7ytf5t7k(v=vs.100).ASPX
Do try the Request.Form to get the hidden field's current value.


Request.Form["Hiddenfieldname"].ToString();



For Example if you have a hidden field like

<asp:HiddenField name="hdnResultValue" Value="0" runat="server" />


than you can get the value like


C#
Request.Form["hdnResultValue"].ToString();


Hope it will help.
 
Share this answer
 
See
You are using both client and server side method
For this You have to call client side in client event and server side in server event
For this I have showing modifited code in following below

XML
<div>
       <asp:HiddenField ID="hdnResultValue" runat="server" />
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="getvalue();"/>
   </div>



Server button click event should for test purpose is that

C#
protected void Button1_Click(object sender, EventArgs e)
   {
      // Page.ClientScript.RegisterStartupScript(this.GetType(), null, "getvalue();", true);
       string codeBehindValue = hdnResultValue.Value;
       Response.Write(codeBehindValue);
   }
 
Share this answer
 
Comments
shubhangi waghmare 14-Aug-15 7:35am    
I am using one java script function like
function ValidateParameters(obj) {
if (obj == "Update" || obj == "Insert") {
//hdnIsCommandClick
document.getElementById("MainContent_hdnIsCommandClick").value = "True";
}
}

Here if i am clicking on button two times, record is adding two times. Is there any issue in hidden field.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900