单击主页面上的按钮可从内容页面

时间:2017-10-30 15:14:30

标签: c# asp.net master-pages

我在Master页面上有一个按钮,当它被单击时,它会在主页面上调用一个需要多个参数的方法。参数的值存储在母版页和内容页面上的文本框和下拉列表中。我可以从母版页的控件中获取值,但我不确定如何从内容页面上的控件中检索值。

我花了最后一天在这里和其他网站上查看各种答案,但是还没有找到适合我的解决方案。

我也看到了建议将按钮放在控制页面上的答案,但这意味着要在至少12页上复制代码,除非必须,否则我真的不想离开那条路线。

我忘了说我已经在内容页面中使用了主参考标记。

修改

如果我在内容页面中创建一个类并设置值,那么检索母版页中值的最佳方法是什么。假设有可能吗?

1 个答案:

答案 0 :(得分:0)

I suggest you add a public method like SetYourDropdownValue(string val) to your master page. In the load-eventhandler from the control page you hand over the selected value from the dropdown to your master page.

MyMainPage m = (MyMainPage)Master;
m.SetYourDropdownValue("your value");

There is an alternative way tho: Master Pages Reference.

You can set following reference tag in your control page markup:

<%@ MasterType virtualPath="~/MasterPage.master"%>

Then it will be even easier:

Master.SetYourDropdownValue("your value");

In the SetYourDropdownValue-Method you save the value to a variable. On the click eventhandler in your master page, you can then retreive the value from your variable.

Instead of passing a string you could also pass one or more controls, so you would just have to save the reference to it, instead of saving a value.