开发者

How to submit data from Web User Control

I have a form that I put on the Web User Control, but "Submit" button is on the parent page.

The form contains user informatio开发者_开发技巧n. That same form displays for admins to edit a user and for the user to register and update profile.

I wanted to have the form in only 1 place to display in those 3 places.

The admin page will have a little more information on it, along with the "user info" control.

how can i reference items on the control page from the parent page?

am i doing this wrong?


You could make the control's elements (e.g. a contained TextBox) accessible through properties.

E.g.

public partial class MyControl : UserControl
{
    public string MyText 
    {
        get { return MyTextBox.Text; }
        set { MyTextBox.Text = value; }
    }
}

Then, access the properties from the page that contains the web user control.


You can use the FindControl method and cast using CType. Here is an example...

    Dim objUserControl As UserControl = CType(Me.FindControl("IdOfUserControl"), UserControl)
    Dim strTextOfLabelInUserControl As String = CType(objUserControl.FindControl("IdOfLabelInUserControl"), Label).Text

or better yet...

    Dim strTextOfLabelInUserControl as String = CType(IdOfUserControl.FindControl("IdOfLabelInUserControl"), Label).Text

However, you may want to consider using Uwe Keim's approach to avoid the Overhead of casting and the FindControl methods. His translated into VB .Net is this...

Public Partial Class WebUserControl1
Inherits System.Web.UI.UserControl

Public Property TextOfLabel() As String
    Get
        Return IdOfLabelInUserControl.Text
    End Get
    Set(ByVal value As String)
        IdOfLabelInUserControl.Text = value
    End Set
End Property
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜