开发者

How to Reference TextBox in One UserControl from Another UserControl

开发者 https://www.devze.com 2023-02-05 19:56 出处:网络
I have two UserControls on a MasterPage. DataEntryUC contains several TextBoxes and DropDownList. NavSaveUC contains navigation buttons. When th开发者_如何学JAVAe user clicks on a navigation button, I

I have two UserControls on a MasterPage. DataEntryUC contains several TextBoxes and DropDownList. NavSaveUC contains navigation buttons. When th开发者_如何学JAVAe user clicks on a navigation button, I will be saving the data entered into DataEntryUC from the NavSaveUC UserControl.

I have a couple of tables in my DB that contain stored procedure names, control names, control types, SqlDbTypes, etc.... that correlate with DataEntryUC.

How do I reference a text box that is on DataEntryUC from NavSaveUC?

I have been working on the following code from NavSaveUC with no luck.

Dim MyControlName = "txtFirstName"
Dim MyControlType = "TextBox"
Dim MyStringValue as String
Dim tmpTxtControl as TextBox
Dim tmpDdlControl as DropDownList

Select Case MyControlType
    Case "TextBox"
        tmpTxtControl = CType(Page.FindControl(MyControlName, TextBox)
        MyStringValue = tmpTxtControl.Text                  
    Case "DropDownList"
        tmpDdlControl = CType(Page.FindControl(MyControlName, DropDownList)
        MyStringValue = tmpDdlControl.SelectedValue
End Select

Thanks!


Expose the textbox as a property of the user control and access it that way. This type of dependency is not a best practice though.


If the one user control is embedded inside the other user control, then simply expose it as a property of the control. If, however, both user controls separately exist on the same page, the controls should not interact directly with each other to avoid a dependency between the controls.

Instead, you should use the page as an intermediary between the two controls. For instance, NavSaveUC can raise an event with data in it's event argument when an action is performed. The page can handle the event from NavSaveUC. If DataEntryUC exposes the text box or text box's text property as a public property, the page's event handler can get data from NavSaveUC's event argument and place it in DataEntryUC's text box.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号