简体   繁体   English

我如何才能将一个控件中的文本框的值获取到另一个控件中

[英]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.

相关问题 如何从一个用户控件到另一个用户控件获取文本框信息 - How to get Textbox information from one user control to another 从一个视图切换到另一个视图时,如何保持用户控件的 Textbox Text 属性的值? - How do I persist the value of the Textbox Text property of a user control when switching from one view to another? 是否可以将一个文本框控件的值绑定到另一个文本块控件? - Is it possible to bind the value of one textbox control to another textblock control? 如何获得控件的图像到另一个控件? - How can I get an image of control to another control? 我如何在UpdatePanel中获取ASP文本框控件的值 - How can i get the values of asp textbox control in UpdatePanel 如何通过单击另一个用户控件中的按钮来在用户控件的文本框中显示一些文本? - How can I show some text on a textbox in a user control by clicking a button in another user control? 搜索文本框控件(改编自另一个控件)无法触发 textchanged,也无法设置或获取文本 WPF - Search TextBox Control (adapted from another control) cannot fire textchanged, nor can I set or get text WPF 如何将数据绑定从一个控件复制到另一个控件? - How can I copy the databinding from one control to another? 如何将文本从一个文本控件包装到另一个文本控件? - How can I wrap text from one text control to another? 如何让 numericupdown 控件在输入时将其值传递给另一个控件,而不必按 Enter 键或单击另一个控件 - How to get numericupdown control to pass its value to a another control as I am typing not by having to hit Enter or click on another control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM