C#

Monday, February 3, 2014

HiddenField Control in Asp.net Using C#.net

Introduction:

The HiddenField control provides you with a way to store information in the page without displaying it. For example, you might store a user-preference setting in aHiddenField control. To put information into a HiddenField control, you set its Value property to the value you want to store between postbacks.
As with any other Web server control, the information in a HiddenField control is available during postback. The information is not persisted outside the page.

HiddenField Control and Security:

The information in a HiddenField control is not displayed when the browser renders the page, but users can see the contents of the control by viewing the page's source. Therefore, do not store sensitive information in a HiddenField control, such as user IDs, passwords, or credit card information.

Detecting Changed Data

Users can change the value of a HiddenField control before the page is posted back to the server, potentially compromising the information. To help you detect this type of change, the HiddenField control raises a ValueChanged event if the value of the control changes between postbacks. If the values of the HiddenField controls contain sensitive information or are necessary for your application to function properly, you should handle this event for all of the HiddenField controls on the page.

<asp:HiddenField id="HiddenField1" runat="server" value="1"/>

Getting HiddenFeild Values Using C#.net:

<html>
<head>

    <script language="C#" runat="server">

       void Button1_Click(object sender, EventArgs e)
       {
            if (HiddenField1.Value == String.Empty)
               HiddenField1.Value = "0";
            
            //Increment the hidden field value by 1
            HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value)+1).ToString();
             
            Label1.Text = HiddenField1.Value;
       }

    </script>

</head>
<body>

    <h3><font face="Verdana">HiddenField</font></h3>

    <form runat=server>
        <asp:HiddenField id=HiddenField1 runat=Server />

        <asp:Button id=Button1 Text="Click Me" onclick="Button1_Click" runat="server" />
        Clicked <asp:Label id=Label1 Text="0" runat=server /> times

    </form>
    
</body>
</html>

No comments:

Post a Comment