简体   繁体   中英

C# ASP.NET Master Page and ChildPage - Assign strings from Child Page into Master Page

Here is the section of my child page file and my master page file respectively. I am going to pass strName, from Child page to master page .I would like to access this Master Page variable in child page and assign child page variable to master page on or before the Page Load. I often get the last selected value but not the current value

For instance , Given the record shows Mary, John , Peter,

if I click Mary , then show none if I click Peter, show Mary if I lick John, show Peter

Would you please suggest and show how to resolve this issue ?

the following is my code

   protected void grdNameSelection_ItemCommand(object sender, GridCommandEventArgs e) 
    {
        RadGrid CurrentGrid = (RadGrid)sender;
        GridData GridSource = PageSource.GrdSrcNameSelection;
        String a = GridSource.state.ToString();

        if (e.CommandName == "SelectCurrent")
        {
            foreach (GridDataItem item in CurrentGrid.SelectedItems)
            {

                RowData row = (RowData)GridSource.Current[item.DataSetIndex];
                string Name = row.Name;
            }
        }
    }
 protected void grdCompanySelection_PreRender(object sender, EventArgs e)
    {
        UserSession.Instance.CurrentName= Name ;

        RadGrid CurrentGrid = (RadGrid)sender;
        GridData GridSource = PageSource.GrdSrcCompanySelection;
        CommandButtonDisplay(GridSource, CurrentGrid);

    }

Master Page

 protected void Page_init(object sender, EventArgs e)
    {

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        lblHeader.Text = UserSession.Instance.CurrentName;

        if (!Page.IsPostBack)
        {  ,,,,

Unfortunately MS hasn't provided a very good way of doing this. You basically have two options.

  1. Access the control from the child page and then set whatever value you need (child page is this in the example):

     Label label1 = this.Master.FindControl("Label1") as Label; label1.Text = "Hello"; 
  2. Preferred provide some kind of setter on the master page. This is "safer" because the child page doesn't directly manipulate the master's controls.

      MyType master = this.Master as MyType; if(master == null) return; master.MyProperty = "something"; // In master page public string MyProperty { get { return label.Text; } set { label.Text = value; } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM