Monday, 28 April 2014

ViewState Example in C# , Hidden Example in ascx,javascipt

 

View State:

<table>
<tr>
<td>ViewState Data:</td><td><b><asp:Label ID="lblString" runat="server"/></b></td>
</tr>
<tr><td></td><td> <asp:Button ID="btnClick" runat="server" Text="Get ViewState Data"
onclick="btnClick_Click"/></td></tr>

</table>

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string str = "Welcome to Aspdotnet-Suresh Site";
if(ViewState["SampleText"]==null)
{
ViewState["SampleText"] = str;
}
}
}
protected void btnClick_Click(object sender, EventArgs e)
{
lblString.Text = ViewState["SampleText"].ToString();
}

output:


http://www.aspdotnet-suresh.com/2012/11/viewstate-in-aspnet-with-example-in-c.html



Hiddenfield:

Examlpe1:

javascript:

function SetHdnField() {
            var x = document.getElementById('<%= hdnSomeField.ClientID %>');
            x.value = 'hi';
            return false;
        }
aspx:

<asp:HiddenField runat="server" ID="hdnSomeField" />
   <br />
   <asp:Button runat="server" ID="btnSetValue" Text="Set Value" OnClientClick="return SetHdnField();" />


Example2:

      

  1. Function test()  
  2. {  
  3.   int i=10;  
  4.   var hiddentest = '';  
  5. document.getElementById('<%= hiddentest.ClientID%>').value = i;  
  6.  }  
Default.aspx
  1. <asp:HiddelFiled runat="server" id="test"></asp:Hiddenfield>  
  2. <asp:textbox runat="server" id="eletext"></asp:textbox>  
The id of the hidden field is used to assign the JavaScript variable value
Code behind Page(.cs):
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. eletext.text=test.value;   
  4. }  
         








No comments:

Post a Comment