Access Text Field in User Control

HI

I have a page which includes a header user control

Page

<%@ Page Language="vb"%>
<%@ Register TagPrefix="HEADER" TagName="HEAD" Src="../lib/_header.ascx" %>
<FORM id="Form1" action="" method="post" runat="server">
<HEADER:HEAD id="HEAD" runat="server"></HEADER:HEAD>
</FORM>


now my header control includes a placeholder
<%@ Control Language="vb" Codebehind="_header.ascx.vb" %>

<ASP:PLACEHOLDER id="HeaderTop" runat="server"></ASP:PLACEHOLDER>

 
a usercontrol gets programmatically added to this placeholder

Dim cnt As New UserControl()
HeaderTop.Controls.Add(LoadControl("~\lib\_mainheader.ascx"))

STIll with me?? good

Now mainheader.ascx looks like this

<%@ Control Language="vb" AutoEventWireup="false" Codebehind="_mainheader.ascx.vb" %>
<%@ Register TagPrefix="SEARCHBOX" TagName="SEARCHBOX" Src="_searchbox.ascx" %>
<SEARCHBOX:SEARCHBOX id="SEARCHBOX" runat="server"></SEARCHBOX:SEARCHBOX>

Searchbox control looks like this

<%@ Control Language="vb" AutoEventWireup="false" Codebehind="_searchbox.ascx.vb" %>
<INPUT  type="text" name="SEARCH_keywords" id="SEARCH_keywords" runat=server>
<INPUT type="submit" id="Submit1" name="Submit1" runat=server>


The row from the trace looks like this HEAD:_ctl1:SEARCHBOX:SEARCH_keywords

What i need to do is access the value of SEARCH_keywords from within the code behind of _header.ascx



Cheers

Len
LVL 10
lengreenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nurbekCommented:
yoh have to desing the searchbox user control according to your preferences

in your searchbox usercontrol

<script language="VB" runat="server">
  Public Property txtSearchBox As String
    Get
      Return txtSearch.Value
    End Get
    Set
      txtSearch.Value = Value
    End Set
  End Property
</script>
<input id="txtSearch"  type="text" runat="server">


now in your main page you can get it value

like

myValue =  SEARCHBOX.txtSearchBox

hope it helps



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nauman_ahmedCommented:
1. Add the following property to the user control:

public string SearchText
{
   get {return this.txtSearch.Text;}
   set {this.txtSearch.Text = value;}

}


2. In the page where you are using the user control, add the following code:

protected MyUserControl MyUserControl1;

3. Access the txtSearch as follows:

   MyUserControl.SearchText = "From Parent";


HTH, Nauman.
0
nauman_ahmedCommented:
Or in VB:

1. Public Property SearchText As String
    Get
      return Me.txtSearch.Text
    End Get
    Set (ByVal Value as String)
      Me.txtSearch.Text = value

    End Set

2. Add the following in Header.ascx code behind:

   Protected MyUserControl1 As MyUserControl

3. Get or Set the value for search textbox:

   MyUserControl1.SearchText = "From Header"

HTH, Nauman.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

lengreenAuthor Commented:
Hi Guys

OK, setting the property in the class should be fine but
when i click on the submit i need to access the value from within the first User Control

Something like

DIM MYSEARCHCONTROL as __Searchbox  = Reference to the Control which was posted back


Dim keywords As String = MYSEARCHCONTROL.txtSearchBox

I cannot create a new Instance of the control otherwise the value will not be correct

Cheers

Len



0
nauman_ahmedCommented:
You dont need to create a new instance. The postback will not erase the value of the search text field from the user control. You can access it like:

Response.Write(MyUserControl.SearchText)

HTH, Nauman.
0
lengreenAuthor Commented:
Hi Again

Thanks for your help, can't be far away from whats wrong but

From within Header.ascx code behind

HeaderTop.Controls.Add(LoadControl("~\lib\_mainheader.ascx"))

my search user control is within the mainheader user control

So

Protected MyUserControl1 As __searchbox

Sub Page_Load(etc)

Dim keywords As String = MyUserControl1.txtSearchBox

gives me an object reference not set  

Cheers

Len

0
nurbekCommented:

<%@ Register TagPrefix="SRCHBOX" TagName="SEARCHBOX" Src="_searchbox.ascx" %>




<SRCHBOX:SEARCHBOX id="myNewNameID"  runat="server"/>


Dim keywords As String = myNewNameID.txtSearchBox



0
nauman_ahmedCommented:
ok buddy here is the sample ;)

http://www.techinet.com/downloads/SampleVBWebApplication.zip.

I think you must have two drives on your PC. Make a virtual directory in d:\projects and unzip the files into that. Browse the application using the URL http://localhost/projects/SampleVBWebApplication/ . Otherwise edit SampleVBWebApplication.vbproj.webinfo in the project directory and update the path of the project.

Here is what I have done:

Header.ascx is a user control containing search.ascx. Search.ascx has a property SearchText that I am calling from Header Page_Load event after adding protected Search1 as Search.

Best, Nauman.
0
lengreenAuthor Commented:
Hi

Still no joy here is my setup

PAGE
 |
 |
 USERCONTROL(HEAD) (_header.ascx)
   ASP:PLACEHOLDER(HeaderTop)
   |
   |
   USERCONTROL (_ctrl1) (_mainheader.ascx) loaded programatically
      |
      |
      USERCONTROL (SEARCHBOX) (_search.ascx)
        HTMLCONTROL(Keywords)




WHen i load the HEAD user control

i have a line of code

if something = true then

HeaderTop.Controls.Add(LoadControl("~\lib\_mainheader.ascx"))

end if

which is fine


but what i need to do just before that line of code executes is to check the posted back keywords value
Im well and truely stumped on this one


cheers

Len
0
lengreenAuthor Commented:
Hi Guys

I have Got round my Problem by not dynamically loading the control

and setting visible to false when not required

i can access by

Dim MYControl As __searchbox = Me.FindControl("HEAD").FindControl("HeaderTop").FindControl("SEARCHBOX")
Dim keywords As String = MYControl.txtSearchBox

thanks for your help as you steered me to a solution thats working

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.