【问题标题】:Passing int and string values from masterpage to content page将 int 和 string 值从母版页传递到内容页
【发布时间】:2014-12-24 11:53:00
【问题描述】:

我的母版页中有一个单选列表,当我选择其中一个时,它们会触发一个事件。

现在此事件不受我的母版页控制,而是由我的内容页控制。 我的问题是,是否可以以某种方式将 int/Strings 从母版页方法传递到内容页方法。

PS 在这种情况下我想将 int i 传递给内容页面方法

这就是我解决问题的方法。

处理事件的母版页代码

public event EventHandler Master_Save;
  ...
public void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    int i=RadioButtonList1.SelectedIndex;        
    if(Master_Save!=null)
    { Master_Save(this, EventArgs.Empty); }
}

和我的内容页面代码来处理事件

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    (this.Page.Master as Pages_MasterPage).Master_Save += new EventHandler(ContentPage_Save);
}

 private void ContentPage_Save(object sender, EventArgs e)
{
    //Code that changes a query   

 }

【问题讨论】:

标签: c# asp.net master-pages content-pages


【解决方案1】:

当然可以,只需定义一个自定义事件 args 类并使用它参数化您的事件:

public class MasterSaveEventArgs : EventArgs
{
    public int Index { get; private set; }
    public MasterSaveEventArgs(int index)
    {
        this.Index = index;
    }
}

然后就用它吧:

public event EventHandler<MasterSaveEventArgs> Master_Save;
...
{ Master_Save(this, new MasterSaveEventArgs(i)); }
...
(this.Page.Master as Pages_MasterPage).Master_Save += ContentPage_Save;
// notice the shortened syntax here
...
private void ContentPage_Save(object sender, MasterSaveEventArgs e)

【讨论】:

  • 感谢您的回复。但是,在我的 EventHandler 行上,我的 PreInt 方法有一个错误。你能帮我弄清楚吗
  • @user3070259,编辑答案。对于未来的情况,指定您遇到的什么错误总是有用的,否则您将让其他人猜测
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多