I want pass values between web user controls without writing any code to the main page which user controls are put on. I do something like that but after doing that I need to click double to pass the value.
我想要在Web用户控件之间传递值,而无需将任何代码写入用户控件所在的主页面。我做了类似的事情,但在这之后我需要点击double来传递值。
The example of what I've done :
我做过的例子:
Department User Control (Code-Behind)
部门用户控制(代码隐藏)
protected void Page_Load(object sender, EventArgs e)
{
int productId = ProductUserControl.selectedProductId;
... doing some bind work with productId
}
Product User Control
产品用户控制
public static int selectedProductId;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void lvDepartments_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "selectDepartment")
{
selectedProductId = int.Parse(e.CommandArgument);
}
}
Thanks in advance...
提前致谢...
2 个解决方案
#1
In your Department User Control you are trying to get the value of selectedProductId before it is set in the Product User Control. That's why you don't get the value you expect until you postback twice.
在“部门用户控制”中,您尝试在“产品用户控件”中设置之前获取selectedProductId的值。这就是为什么在你回发两次之前你没有得到你期望的价值。
You'll need to get it after the Product User Control sets it in the ItemCommand event. Perhaps placing the Department User Control code in the Page_LoadCompleted... though I'm not sure if that will work either.
产品用户控件在ItemCommand事件中设置后,您需要获取它。也许将部门用户控制代码放在Page_LoadCompleted中......虽然我不确定这是否也可以。
Another way to do it is to have Product User Control set a public property in Department User Control instead of having Department User Control try to read a property in Product User Control.
另一种方法是让Product User Control在Department User Control中设置公共属性,而不是让Department User Control尝试读取Product User Control中的属性。
The issue seems to be a Page Lifecycle issue. http://www.robincurry.org/blog/content/binary/o_aspNet_Page_LifeCycle.jpg
该问题似乎是页面生命周期问题。 http://www.robincurry.org/blog/content/binary/o_aspNet_Page_LifeCycle.jpg
I'm sure there's a better way than that as well.
我相信还有比这更好的方法。
#1
In your Department User Control you are trying to get the value of selectedProductId before it is set in the Product User Control. That's why you don't get the value you expect until you postback twice.
在“部门用户控制”中,您尝试在“产品用户控件”中设置之前获取selectedProductId的值。这就是为什么在你回发两次之前你没有得到你期望的价值。
You'll need to get it after the Product User Control sets it in the ItemCommand event. Perhaps placing the Department User Control code in the Page_LoadCompleted... though I'm not sure if that will work either.
产品用户控件在ItemCommand事件中设置后,您需要获取它。也许将部门用户控制代码放在Page_LoadCompleted中......虽然我不确定这是否也可以。
Another way to do it is to have Product User Control set a public property in Department User Control instead of having Department User Control try to read a property in Product User Control.
另一种方法是让Product User Control在Department User Control中设置公共属性,而不是让Department User Control尝试读取Product User Control中的属性。
The issue seems to be a Page Lifecycle issue. http://www.robincurry.org/blog/content/binary/o_aspNet_Page_LifeCycle.jpg
该问题似乎是页面生命周期问题。 http://www.robincurry.org/blog/content/binary/o_aspNet_Page_LifeCycle.jpg
I'm sure there's a better way than that as well.
我相信还有比这更好的方法。
#2
Try using delegates to achieve this more cleanly, example here
尝试使用委托来更清晰地实现这一点,例如这里