cdub
cdub

Reputation: 25751

Grabbing a control from an ascx page in ASP.Net

I have an .ascx user control that is in my search.aspx page. How do I grab a control from the .ascx user control in the search.aspx.cs code behind ?

keywordSearch.Value = "value"; 
// the code behind can't see the keywordSearch control

Upvotes: 4

Views: 1139

Answers (3)

kprobst
kprobst

Reputation: 16651

Normally inner controls are not exposed from templated user controls, because they're declared as protected. You can however expose the control in a public property, like this:

public TextBox CustomerName {
    get { return txt_CustomerName; }
}

Edit: If you need to set the value of the control then you're better off with a property that exposes the value, not the control:

public string CustomerName {
    get { return txt_CustomerName.Text; }
    set { txt_CustomerName.Text = value; }
}

Upvotes: 3

Canavar
Canavar

Reputation: 48098

Try FindControl method to access a control at container page :

((TextBox)Page.FindControl("keywordSearch")).Value = "value";

Upvotes: 1

FlyingDeveloper
FlyingDeveloper

Reputation: 342

You might be able to provide a public (or internal) property in your user control's code behind that allows "getting" the control in the user control. You could then access that property from your page's code behind.

Upvotes: 3

Related Questions