Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,
I am developing a web application in which i need to store a some values in a hiddenfield and i need to access this values in other pages. I need to post huge data and so i prefer using HiddenField than Session or QueryString. I am using the following code.

Default1.aspx

<asp:HiddenField ID="hdnEmailID" runat="server" />


Default1.aspx.cs

C#
protected void LinkGroupSummary_Click(object sender, EventArgs e)
        {            
            hdnEmailID.Value = this.EmailList;
            Server.Transfer("Default2.aspx");

        }


Default2.aspx.cs


C#
if (Page.PreviousPage != null)
                    {
                        HiddenField MyHiddenValue = (HiddenField)Page.PreviousPage.FindControl("hdnEmailID");
                        if (MyHiddenValue != null)
                        {
                            string email = MyHiddenValue.Value;
                        }
                    }


In the Default.aspx.cs page, i am getting MyHiddenField as null always.

Please help.

Thanks in advance,
Sruthi R
Posted
Updated 18-Jun-12 18:30pm
v3

Hi,

make it a server control hiddenfield. Set the PostbackUrl property of the button to the next page and retrieve the information there.

For a good example on how to use the PostBackUrl property take a look at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx.

Page 1:
XML
<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>Button.PostBackUrl Example</title>
</head>
<body>
  <form id="form1" runat="server">

    <h3>Button.PostBackUrl Example</h3>

    Enter a value to post:
    <asp:textbox id="TextBox1"
      runat="Server">
    </asp:textbox>

    <br /><br />

    <asp:button id="Button1"
      text="Post back to this page"
      runat="Server">
    </asp:button>

    <br /><br />

    <asp:button id="Button2"
      text="Post value to another page"
      postbackurl="Button.PostBackUrlPage2cs.aspx"
      runat="Server">
    </asp:button>

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

Button.PostBackUrlPage2cs.aspx:

<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load (object sender, System.EventArgs e)
  {
    string text;

    // Get the value of TextBox1 from the page that
    // posted to this page.
    text = ((TextBox)PreviousPage.FindControl("TextBox1")).Text;

    // Check for an empty string.
    if (text != "")
      PostedLabel.Text = "The string posted from the previous page is "
                         + text + ".";
    else
      PostedLabel.Text = "An empty string was posted from the previous page.";
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>Button.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">

    <h3>Button.PostBackUrl Target Page Example</h3>

    <br />

    <asp:label id="PostedLabel"
       runat="Server">
    </asp:label>

    </form>
</body>
</html>
 
Share this answer
 
XML
Hello freinds,
//use this code

suppose i have 2 page. A.aspx and B.aspx
I have pass value of page a to page b using hidden field
//take hiddenfield on page, one button with hidden property and write one script

<script type="text/javascript">
        function load(id) {
            document.getElementById('<%=HiddenField1.ClientID%>').value = id;
            document.getElementById("<%= ButtonHide.ClientID %>").click();
        }
        </script>



<asp:HiddenField ID="HiddenField2" runat="server" />;
<asp:Button ID="ButtonHide" runat="server" Text="Button" style="display:none;"
                           formnovalidate="true"     onclick="ButtonHide_Click"/>

//in code file of page a
 protected void ButtonHide_Click(object sender, EventArgs e)
    {
        Response.Redirect("PriceDetail.aspx?id="+HiddenField1.Value+"");
    }

now in page b code file

int id = Convert.ToInt32(Request.Params["id"]);
and u get value of hiddenfield
 
Share this answer
 
 
Share this answer
 
v2
How to: Pass Values Between ASP.NET Web Pages[^] should be the perfect link for you to go through. Have a look at how _VIEWSTATE_ automatically passes hidden fields when posting data.
 
Share this answer
 
Comments
[no name] 18-Jun-12 23:56pm    
Good reference. MSDN is the best. My +5.
Pankaj Nikam 19-Jun-12 1:26am    
+5 Awesome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900