[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Access Text Field in User Control

Posted on 2004-11-05
10
Medium Priority
?
308 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:lengreen
  • 4
  • 4
  • 2
10 Comments
 
LVL 13

Accepted Solution

by:
nurbek earned 750 total points
ID: 12503910
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
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12503981
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
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12504021
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 10

Author Comment

by:lengreen
ID: 12504103
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
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12504227
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
 
LVL 10

Author Comment

by:lengreen
ID: 12504368
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
 
LVL 13

Expert Comment

by:nurbek
ID: 12504540

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




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


Dim keywords As String = myNewNameID.txtSearchBox



0
 
LVL 25

Assisted Solution

by:nauman_ahmed
nauman_ahmed earned 750 total points
ID: 12504771
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
 
LVL 10

Author Comment

by:lengreen
ID: 12505547
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
 
LVL 10

Author Comment

by:lengreen
ID: 12506285
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Suggested Courses

612 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