Don
Don

Reputation: 1572

A easy way to pass parameter back and forth in every page in ASP.NET

I want to pass an id back and forth in every page. I can not use session, application variables and database since I want it is base on page. Hidden field if a form exists or url concatenation are what I can think about.

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

Upvotes: 3

Views: 1161

Answers (3)

mshsayem
mshsayem

Reputation: 18018

An idea:

Place a hidden field in the master page:

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

Use this extension method to get/set that 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();

Upvotes: 2

Mahesh Kava
Mahesh Kava

Reputation: 791

setup a public string value in your master page

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

Access the property in your child page

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

Upvotes: 3

Dinand
Dinand

Reputation: 359

You can transfer it with the request url as parameter. example In page 1 you redirect to page 2 with parameter Test

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

In page 2 you get it like:

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

Upvotes: 0

Related Questions