简体   繁体   中英

ASP.NET: Pass value from User Control to page?

I am creating a user control in ASP.NET (using VB) that uses the autocomplete ajax control on a textbox to get a value. Then I want the page to post back and run some code according to whatever value is passed to it from this control. Problem is, I'm not exactly sure how to do this. I'm sure it's easy and I should know, but I don't.

Thanks in advance!

In your user control expose a property for the value

Public Property SomeValue() As String
Get
    Return textbox1.Text
End Get
End Property

Then in your aspx page load, just reference the user control's value.

userControl1.SomeValue

Edit, I just tried changing my syntax to vb.net, I don't actually know vb, so that syntax may or may not be right.

((NameOfPage)this.Page).VariableOnPage = this.Foobar;

In the code-behind on your user-control expose a property eg

public TextBox UserControlTextBox
{
    return this.TextBoxInstance;
}

Then from you page just call

UserControlInstance.UserControlTextBox.Text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM