.NET FAQs Unleashed!
 
    
    

How can you pass data to a user control?

User controls may be comprised of several server side controls. In some scenarios, developers may need to set the properties of the internal controls from the outer page. The following may be carried out:

Say the user control’s code behind file is uc.ascx.cs, open this file and then add a public method or property that the page would invoke.

Example (VB.NET):

 

Public Sub SetTheLabelText(ByVal val As String)

Label1.Text = val

End Sub.

Now in the page’s code behind file, declare a reference to the user control.

Protected WithEvents TheControl As TheUserControl

Next, the user control’s custom method may be invoked from within the page.

TheControl.SetTheLabelText("That was an easy interview")



ASP.NET Expression Symbols   Web Service in ASP.NET   Pass data to Web Control   Zipping a folder in ASP.NET   Check user online or offline   Display Dynamic Tooltips   Dynamic Server Side Tables   Capture Gridview Row Index   Improve ASP.NET Performance   Refresh page in another page   Step into DLL Source Code   Insert Non-Ascii Characters in a Database   Contract in WCF   Dependency Injection   Asp.net triggering a Postback   Cyclomatic Complexity   ~ Character in ASP.NET   Web Application Project VS. Website Project   Optimistic Locking VS. Pessimistic Locking   System.Net.Mail VS. System.Web.Mail   Breakpoints in debugging  

More Interview Questions...