BJM1M
asked on
Finding a user control - Three user controls loaded via code behind - Cannot find the textbox value of the search control.
I have a web page with a header, search and results controls.
The header control presents a logo at the top of the page, the search control presents a textbox and a button which when clicked will call call a results control which queries a SQL table and returns a populated datagrid.
The problem: I want to pass the string entered in the textbox of the search control to the results datagrid. I've used the following code in the search.ascx:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSrch.Click, btnSearch.Click
Dim ctlControl As Control
ctlControl = LoadControl("results.ascx" )
plhResults.Controls.Add(ct lControl)
End Sub
Public ReadOnly Property srchTxt() As String
Get
Return TbSrch.Text
End Get
End Property
End Class
in the results.ascx:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim srchTxt As String
Dim _myControl As Control = Page.FindControl("UCtlsear ch")
Dim _myControlType As Type = _myControl.GetType()
Dim _myUC_SrchTxt As PropertyInfo = _myControlType.GetProperty ("srchTxt" )
srchTxt = _myUC_SrchTxt.GetValue(_my Control, Nothing)
Dim ds As DataSet
Dim conn As String
Dim spName As String = "spGetPolicy"
conn = ConfigurationSettings.AppS ettings("D BQQStatus" )
Dim storedParams(1) As SqlParameter
storedParams = SqlHelperParameterCache.Ge tSpParamet erSet(conn , spName)
storedParams(0).Value = srchTxt
ds = SqlHelper.ExecuteDataset(c onn, CommandType.StoredProcedur e, spName, storedParams)
DgPolicy.DataSource = ds
'DgPolicy.DataMember = "Policies"
DgPolicy.DataBind()
End Sub
UCtlsearch is the id of the search control which is loaded from the header control.
The FindControl statement returns nothing.
Any ideas? I can supply the full code if required
The header control presents a logo at the top of the page, the search control presents a textbox and a button which when clicked will call call a results control which queries a SQL table and returns a populated datagrid.
The problem: I want to pass the string entered in the textbox of the search control to the results datagrid. I've used the following code in the search.ascx:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSrch.Click, btnSearch.Click
Dim ctlControl As Control
ctlControl = LoadControl("results.ascx"
plhResults.Controls.Add(ct
End Sub
Public ReadOnly Property srchTxt() As String
Get
Return TbSrch.Text
End Get
End Property
End Class
in the results.ascx:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim srchTxt As String
Dim _myControl As Control = Page.FindControl("UCtlsear
Dim _myControlType As Type = _myControl.GetType()
Dim _myUC_SrchTxt As PropertyInfo = _myControlType.GetProperty
srchTxt = _myUC_SrchTxt.GetValue(_my
Dim ds As DataSet
Dim conn As String
Dim spName As String = "spGetPolicy"
conn = ConfigurationSettings.AppS
Dim storedParams(1) As SqlParameter
storedParams = SqlHelperParameterCache.Ge
storedParams(0).Value = srchTxt
ds = SqlHelper.ExecuteDataset(c
DgPolicy.DataSource = ds
'DgPolicy.DataMember = "Policies"
DgPolicy.DataBind()
End Sub
UCtlsearch is the id of the search control which is loaded from the header control.
The FindControl statement returns nothing.
Any ideas? I can supply the full code if required
Page.FindControl requires ID of the TextBox after the page has been render. & Since it is a UserControl ID would be slightly different then what you kept before:
If user control name is: UCtlsearch
& TextBox in UC is : srchTxt
Run time ID of UC : UCtlsearch1_srchTxt
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
'replace this 4 line with line shown below:
Dim _myControl As Control = Page.FindControl("UCtlsear ch")
Dim _myControlType As Type = _myControl.GetType()
Dim _myUC_SrchTxt As PropertyInfo = _myControlType.GetProperty ("srchTxt" )
srchTxt = _myUC_SrchTxt.GetValue(_my Control, Nothing)
'----
Dim _myUC_SrchTxt As TextBox = Page.FindControl("UCtlsear ch1_srchTx t")
srchTxt = _myUC_SrchTxt.Text
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
if that dosn't help then.. do View Source & Find 'srchTxt' & check out how exactly the ID of srchTxt is changing
-tushar
If user control name is: UCtlsearch
& TextBox in UC is : srchTxt
Run time ID of UC : UCtlsearch1_srchTxt
--------------------------
'replace this 4 line with line shown below:
Dim _myControl As Control = Page.FindControl("UCtlsear
Dim _myControlType As Type = _myControl.GetType()
Dim _myUC_SrchTxt As PropertyInfo = _myControlType.GetProperty
srchTxt = _myUC_SrchTxt.GetValue(_my
'----
Dim _myUC_SrchTxt As TextBox = Page.FindControl("UCtlsear
srchTxt = _myUC_SrchTxt.Text
--------------------------
if that dosn't help then.. do View Source & Find 'srchTxt' & check out how exactly the ID of srchTxt is changing
-tushar
ASKER
Neither of the above suggestions work. Even if I search for the ID of "CtlHeader__ctl0_TbSrch" which is what is shown in the source of the page when run.
Just to clarify,
The first page called "srchstat.aspx" loads the user control header with an UCtlHeader by using code behind. The HTML is:
<%@ Register TagPrefix="QuickQuote" TagName="Header" src="../controls/qqhead.as cx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="srchstat.aspx. vb" Inherits="qqsts.srchstat"% >
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScri pt">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout ">
<form id="Form1" method="post" runat="server">
<%-- Header of Main Page --%>
<QUICKQUOTE:HEADER id="UCtlHeader" Runat="Server"></QUICKQUOT E:HEADER>
</form>
</body>
</HTML>
The code behind for "srchstat.aspx" is
The HTML for qqhead.ascx is
<%@ Register TagPrefix="uc1" TagName="UCtlsearch" Src="search.ascx" %>
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="qqhead.ascx.vb " Inherits="qqsts.qqhead" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:image id="compLogo" runat="server"></asp:image ><asp:plac eholder id="plhSearch" Runat="server"></asp:place holder>
The code behine for qqhead.ascx
Public Class qqhead
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Protected WithEvents compLogo As System.Web.UI.WebControls. Image
Protected WithEvents plhSearch As System.Web.UI.WebControls. PlaceHolde r
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim ctlControl As Control
'Check if o.k to proceed based on security check
If security.check(Request.Que ryString(" ID")) = False Then
Response.Write(security.Er rMsgType(" DENIED"))
compLogo.Visible = False
Exit Sub
End If
compLogo.ImageUrl = "../img/" + MdbQuery.FLookup("DBData", "LogoFileName", "Security", "SecurityString = '" & Request.QueryString("ID") & "'")
ctlControl = LoadControl("search.ascx")
plhSearch.Controls.Add(ctl Control)
End Sub
End Class
Notice I am using a PLACEHOLDER
The results.ascx is as per the code in the original post.
Does the use of the Placeholder plhSearch affect the use of the FindControl?
Just to clarify,
The first page called "srchstat.aspx" loads the user control header with an UCtlHeader by using code behind. The HTML is:
<%@ Register TagPrefix="QuickQuote" TagName="Header" src="../controls/qqhead.as
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="srchstat.aspx.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScri
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout
<form id="Form1" method="post" runat="server">
<%-- Header of Main Page --%>
<QUICKQUOTE:HEADER id="UCtlHeader" Runat="Server"></QUICKQUOT
</form>
</body>
</HTML>
The code behind for "srchstat.aspx" is
The HTML for qqhead.ascx is
<%@ Register TagPrefix="uc1" TagName="UCtlsearch" Src="search.ascx" %>
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="qqhead.ascx.vb
<asp:image id="compLogo" runat="server"></asp:image
The code behine for qqhead.ascx
Public Class qqhead
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Protected WithEvents compLogo As System.Web.UI.WebControls.
Protected WithEvents plhSearch As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim ctlControl As Control
'Check if o.k to proceed based on security check
If security.check(Request.Que
Response.Write(security.Er
compLogo.Visible = False
Exit Sub
End If
compLogo.ImageUrl = "../img/" + MdbQuery.FLookup("DBData",
ctlControl = LoadControl("search.ascx")
plhSearch.Controls.Add(ctl
End Sub
End Class
Notice I am using a PLACEHOLDER
The results.ascx is as per the code in the original post.
Does the use of the Placeholder plhSearch affect the use of the FindControl?
Try to give ID to the search.ascx
ctlControl = LoadControl("search.ascx")
ctlControl.ID = "UCtlsearch"
plhSearch.Controls.Add(ctl Control)
Then search from the header
UCtlsearch ctl = CType( Page.UCtlHeader.FindContro l( "UCtlsearch" ), UCtlsearch )
Dim srchTxt As String = ctl.srchTxt
ctlControl = LoadControl("search.ascx")
ctlControl.ID = "UCtlsearch"
plhSearch.Controls.Add(ctl
Then search from the header
UCtlsearch ctl = CType( Page.UCtlHeader.FindContro
Dim srchTxt As String = ctl.srchTxt
ASKER
Placing the code:
UCtlsearch ctl = CType( Page.UCtlHeader.FindContro l( "UCtlsearch" ), UCtlsearch )
Dim srchTxt As String = ctl.srchTxt
in the vb code behind of results.aspx does not work as it doesn't know anything about UCtlsearch and UCtlHeader as they are not declared.
UCtlsearch ctl = CType( Page.UCtlHeader.FindContro
Dim srchTxt As String = ctl.srchTxt
in the vb code behind of results.aspx does not work as it doesn't know anything about UCtlsearch and UCtlHeader as they are not declared.
Declare UCtlHeader in srchstat.aspx, like this
Protected WithEvents UCtlHeader As System.Web.UI.UserControl
the same as ID in this line
<QUICKQUOTE:HEADER id="UCtlHeader" Runat="Server"></QUICKQUOT
And within result.ascx
' I assumed class name in code behind search.ascx is UCtlsearch
UCtlsearch ctl = CType( Page.UCtlHeader.FindContro
Dim srchTxt As String = ctl.srchTxt
Set ID property when loading search.ascx in qqhead.ascx
ctlControl = LoadControl("search.ascx")
ctlControl.ID = "UCtlsearch"
plhSearch.Controls.Add(ctl
ASKER
UCtlsearch and UCtlHeader are not defined in result.ascx do you want these to be defined? If not the code beloe will not work
UCtlsearch ctl = CType( Page.UCtlHeader.FindContro l( "UCtlsearch" ), UCtlsearch )
Dim srchTxt As String = ctl.srchTxt
Giving the control an ID in qqhead.ascx is fine...
UCtlsearch ctl = CType( Page.UCtlHeader.FindContro
Dim srchTxt As String = ctl.srchTxt
Giving the control an ID in qqhead.ascx is fine...
Sorry, I missed out something that make it doesn't work
declare UCtlHeader in srchstat.aspx, the variable name must be the same with the id used in aspx
Public WithEvents UCtlHeader As System.Web.UI.UserControl
Then in result.ascx
' I assumed class name in code behind search.ascx is UCtlsearch
UCtlsearch ctl = CType( Page, srchstat ).UCtlHeader.FindControl( "UCtlsearch" )
Dim srchTxt As String = ctl.srchTxt
I don't know class name defined in code behind search.ascx so I assumed it is UCtlsearch. That should work, I have tested it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
YES!
YES!
Thanks - IT WORKS!
Is there anywhere that you can point me to , so that I can read up on this area of control manipulation?
Regards & thanks again
Brian
YES!
Thanks - IT WORKS!
Is there anywhere that you can point me to , so that I can read up on this area of control manipulation?
Regards & thanks again
Brian
Well, Brian..no problem. glad your problem solved. I gotta go for a few hours and come back with some articles that might be useful for you.
Cheers!
If the UCtlsearch is loaded and added to header's control collection you can't find it from the Page. Try to find in header instead, something like this:
UCtlHeader.FindControl( "UCtlsearch" )