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>

Introduction: 
In this article you will come to know Enable/Disable Asp.net server Controls using Javascript.


Following JavaScript is used to Enable/Disable Server Controls:  

  

<script language="javascript" type="text/javascript">

        function SetButtonStatus() {
            var txt = document.getElementById('txtTextBox');
            if (txt.value.length >= 1)
                document.getElementById('btnsubmit').disabled = false;
            else
                document.getElementById('btnsubmit').disabled = true;
        }
</script>


Implementation:
Add a new webform and design the .aspx page as shown below:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script language="javascript" type="text/javascript">
        function SetButtonStatus() {
            var txt = document.getElementById('txtTextBox');
            if (txt.value.length >= 1)
                document.getElementById('btnsubmit').disabled = false;
            else
                document.getElementById('btnsubmit').disabled = true;
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtTextBox" runat="server"onkeyup="SetButtonStatus(this,'btnsubmit')"></asp:TextBox><asp:ButtonID="btnsubmit" runat="server" Text="Submit" />
    </div>
    </form>
</body>

</html>