Easy way to pass parameter back and forth on every page in ASP.NET

I want to pass id back and forth on every page. I cannot use session, application variables and database as I want the database to be on the page. Hidden field, if a form or link concatenation exists, I can think of it.

Is there an easy way to get and send this id without manually adding it to the url or hidden field on every page? For example, use a master page or URL rewriting method.

+3


source to share


3 answers


Idea:

Place a hidden field on your home page:

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

      

Use this extension method to get / set this value from every page:



public static class Util
{
    public static string GetCrossPageValue(this Page page)
    {
        if (page == null || page.Master == null) return null;
        var hf = page.Master.FindControl("hdCrossPageValue") as HiddenField;
        return hf == null ? null : hf.Value;
    }
    public static void SetCrossPageValue(this Page page, string value)
    {
        if (page == null || page.Master == null) return;
        var hf = page.Master.FindControl("hdCrossPageValue") as HiddenField;
        if (hf != null)
        {
            hf.Value = value;
        }
    }
}

      

Like this:

this.SetCrossPageValue("my cross page value");
var crossPageValue = this.GetCrossPageValue();

      

+2


source


set up a public string value on the master page

Public partial class MasterPage:System.Web.UI.MasterPage
{
    public string myValue
    {
        get{return "Master page string value" ;}
        set {}
    }
}

      



Accessing a resource on your child page

protected void Page_Load(object sender, EventArgs e)
{
    MasterPage mp = (MasterPage) Page.Master;
    myLabel.text = mp.MyValue
}

      

+3


source


You can transfer it with the request url as a parameter. example On page 1 you are redirected to page 2 using the Test parameter

Response.Redirect("Page2.aspx?param1=Test");

      

On page 2 you get this:

if (Request.QueryString["param1"] != null)
  var param1 = Request.QueryString["param1"];

      

0


source







All Articles