Link to home
Start Free TrialLog in
Avatar of BJM1M
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(ctlControl)

       
    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("UCtlsearch")
        Dim _myControlType As Type = _myControl.GetType()
        Dim _myUC_SrchTxt As PropertyInfo = _myControlType.GetProperty("srchTxt")
        srchTxt = _myUC_SrchTxt.GetValue(_myControl, Nothing)

        Dim ds As DataSet
        Dim conn As String
        Dim spName As String = "spGetPolicy"
        conn = ConfigurationSettings.AppSettings("DBQQStatus")
        Dim storedParams(1) As SqlParameter
        storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName)
        storedParams(0).Value = srchTxt


        ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, 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
Avatar of ihenry
ihenry


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" )
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("UCtlsearch")
        Dim _myControlType As Type = _myControl.GetType()
        Dim _myUC_SrchTxt As PropertyInfo = _myControlType.GetProperty("srchTxt")
        srchTxt = _myUC_SrchTxt.GetValue(_myControl, Nothing)

'----
Dim _myUC_SrchTxt As TextBox = Page.FindControl("UCtlsearch1_srchTxt")
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
Avatar of BJM1M

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.ascx" %>
<%@ 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_defaultClientScript">
            <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"></QUICKQUOTE: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:placeholder id="plhSearch" Runat="server"></asp:placeholder>

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.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents compLogo As System.Web.UI.WebControls.Image
    Protected WithEvents plhSearch As System.Web.UI.WebControls.PlaceHolder

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration 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.QueryString("ID")) = False Then
            Response.Write(security.ErrMsgType("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(ctlControl)
    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(ctlControl)

Then search from the header

        UCtlsearch ctl = CType( Page.UCtlHeader.FindControl( "UCtlsearch" ), UCtlsearch )
        Dim srchTxt As String = ctl.srchTxt

Avatar of BJM1M

ASKER

Placing the code:

 UCtlsearch ctl = CType( Page.UCtlHeader.FindControl( "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.


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"></QUICKQUOTE:HEADER>

And within result.ascx

       ' I assumed class name in code behind search.ascx is UCtlsearch
        UCtlsearch ctl = CType( Page.UCtlHeader.FindControl( "UCtlsearch" ), UCtlsearch )
        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(ctlControl)
Avatar of BJM1M

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.FindControl( "UCtlsearch" ), UCtlsearch )
        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
Avatar of ihenry
ihenry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BJM1M

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

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!