Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Saturday, 30 November 2013

Introduction: in this article you will get the trick to read value from JavaScript and set in asp.net variable.
Description: You need to create a html hidden field on page and set its value on client side using JavaScript and then read html hidden field using Request.Forms["hiddenfieldname"]

Implementation: In the design page(.aspx) create the form as:

<%@ Page Language="C#" %>
<script runat="server">
    
string var ;
 
    void b
tn_Click(object sender, EventArgs e)
    {
        var = Request.Form["Hidden1"];
        MessageField.Text = var ;
    }

</script>
<head runat="server">
    
<title></title>
</head>
<body>
    <form id="form1" runat="server">
    
<div>
        
<input id="Hidden1" name="Hidden1" type="Hidden"
            runat
="server" value="old value" />
        <
input type="button"
            onclick
="alert(document.getElementById('Hidden1').value)"
            value
="Read Value From Client">
      
        
<input type="button"
            onclick
="document.getElementById('Hidden1').value = 'New Value'"
            value
="Set Value From Client">
      
        
<asp:Button runat="server" ID="btn"
            OnClick
="btn_Click" Text="Read Value From Server Side" /> 
    </
div>
    
<p>
        
<asp:Literal runat="server" ID="MessageField" />
    </
p>
    
</form>
</body>
</html>

Categories: , ,

0 comments:

Post a Comment