[英]How can i get a value of textbox located in one control into another control
I have 2 Controls - ProductInfo and Distribution. 我有2个控件-ProductInfo和Distribution。
On the Product Info Controls there are these text boxs - txtPrice, txtDate, txtFreq On the Distribution control, i would like to get the value of above text boxes. 在产品信息控件上,有以下文本框-txtPrice,txtDate,txtFreq在“分发”控件上,我想获取上述文本框的值。 I tried :
我试过了 :
BasePage.FindControl("txtPrice")
But it return null. 但它返回null。 Please help.
请帮忙。
Thanks, 谢谢,
I would create properties in the ProductInfo control to expose the value of txtPrice, like this: 我将在ProductInfo控件中创建属性以公开txtPrice的值,如下所示:
public decimal Price
{
get { return Decimal.Parse(txtPrice.Text); }
}
And then in the other user control, try something like this: 然后在其他用户控件中,尝试如下操作:
ProductInfo prod = Page.FindControl("OtherUserControl") as ProductInfo;
if (prod != null)
{
decimal price = prod.Price;
}
Recursive method 递归方法
You may need to use recursion to find the ProductInfo
control, and if you do something like this should work: 您可能需要使用递归来查找
ProductInfo
控件,并且如果执行以下操作应该可以:
private Control FindControlRecursive(string controlID, Control parentCtrl)
{
foreach (Control ctrl in parentCtrl.Controls)
{
if (ctrl.ID == controlID)
return ctrl;
FindControlRecursive(controlID, ctrl);
}
return null;
}
Using FindControlRecursive
: 使用
FindControlRecursive
:
ProductInfo prod = FindControlRecursive("OtherUserControl", Page) as ProductInfo;
if (prod != null)
{
decimal price = prod.Price;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.