ASP.NET How you use access a vaule used on the main form, from within a user control

Posted on 2011-10-10
Last Modified: 2012-05-12
Hi I have a user control within a web page. The page is filled with data using the sqldatasource.

On the main page I am using Text='<%# Eval("ShortDescription") %>'
And from within the usercontrol on the same page I want to be able to get the ShortDescription on the parent page.

I know i could probable do this using varibles but there must be an easy way!!!
Question by:taz8020
    LVL 83

    Accepted Solution

    LVL 12

    Assisted Solution

    by:Ramkisan Jagtap
    u can use webcontrol.findcontrol("controlid") method to find ur control. Then access its text.
    LVL 3

    Author Comment

    Thanks both I can see how both these would work, before i posted this i tried puting the value in a session varible.

    My problem is my label Called 'LabelDesc' is in a table Called 'ProductTable' which is in a form called 'formview1'

    How do i access it? and which out of the 3 options is best?

    Session("ProductDescpription")  =  LabelDesc.text       does not work coz in a form
    Session("ProductDescpription")  =  formview1.LabelDesc.text      does not work
    Dim MyLabel as Label = CType(webcontrol.findcontrol("LabelDesc"),Label) does not work

    Please help.

    LVL 83

    Expert Comment

    Use the following

    Dim MyLabel As Label = FormView1.Findcontrol("ProductTable").FindControl("LabelDesc")

    Remember to add error handling
    LVL 3

    Author Comment

    CodeCruiser, Once again thank you. Just to finish I now see how both work. Which would you say is best

    Public ReadOnly Property ProductDescpription() As String
     Return _ProductDescpription
    End Get
    End Property


    Dim MyLabel As Label = FormView1.FindControl("LabelDesc")
    Session("ProductDescpription") = MyLabel.Text
    LVL 83

    Expert Comment

    You are welcome.

    Both approaches are fine. With the second approach, you would need to add error handling to make sure that a label is found before you try to access it.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now