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:
output:
http://www.aspdotnet-suresh.com/2012/11/viewstate-in-aspnet-with-example-in-c.html
Hiddenfield:
Examlpe1:
javascript:
Collapse | Copy Code
function SetHdnField() {
var x = document.getElementById('<%= hdnSomeField.ClientID %>');
x.value = 'hi';
return false;
}
aspx:
Collapse | Copy Code
<asp:HiddenField runat="server" ID="hdnSomeField" />
<br />
<asp:Button runat="server" ID="btnSetValue" Text="Set Value" OnClientClick="return SetHdnField();" />
Example2:
- Function test()
- {
- int i=10;
- var hiddentest = '';
- document.getElementById('<%= hiddentest.ClientID%>').value = i;
- }
- <asp:HiddelFiled runat="server" id="test"></asp:Hiddenfield>
- <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):
- protected void Page_Load(object sender, EventArgs e)
- {
- eletext.text=test.value;
- }
No comments:
Post a Comment