Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

UserControls

Hi,

I created two UserControls called CalvertGrade1 and CalvertGrade2. I have code that populates the data into a GridView control for each UserControl.

I have my main page called index.aspx which Registers both UserControls on the index page. However when i run the index.aspx page i receive an error saying that TextBox1 is not declared. TextBox1 is on the index.aspx page and have it on both UserControls.

I can supply code if needed.

Please help!!
0
asp_net2
Asked:
asp_net2
  • 19
  • 11
1 Solution
 
crazymanCommented:
Code needed....
0
 
asp_net2Author Commented:
INDEX.ASPX:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.SqlTypes" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts.WebPart" %>
<%@ Register Src="~/CalvertGrade1.ascx" TagName="CalvertGrade1" TagPrefix="uc1"%>
<%@ Register Src="~/CalvertGrade2.ascx" TagName="CalvertGrade2" TagPrefix="uc2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form2" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Find Data" />
    <br />
    <br />        

<asp:WebPartZone ID="WebPartZone1" runat="server">
<ZoneTemplate>
<uc1:CalvertGrade1 ID="GridView1" runat="server"></uc1:CalvertGrade1>
</ZoneTemplate>      
</asp:WebPartZone>  

<br />
<br />
     
<asp:WebPartZone ID="WebPartZone2" runat="server">      
<ZoneTemplate>
<uc2:CalvertGrade2 ID="GridView2" runat="server"></uc2:CalvertGrade2>
</ZoneTemplate>  
</asp:WebPartZone>  
   
    </div>
    </form>
</body>
</html>
0
 
asp_net2Author Commented:
CALVERTGRADE1.ASCX:

<%@ Control Language="VB" ClassName="CalvertGrade1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.SqlTypes" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts.WebPart" %>

Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("PSSA").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connectionString)

        Dim dtGrade1 As DataTable = New DataTable()

        Dim gradeComm As SqlCommand
        gradeComm = New SqlCommand("WebPart1Test", conn)
        gradeComm.CommandType = CommandType.StoredProcedure
       
        ' Create Parameter for Querystring
        gradeComm.Parameters.Add("@stid", SqlDbType.Int)
        gradeComm.Parameters("@stid").Value = Request("TextBox1.Text")
       
        Dim adapter As SqlDataAdapter

        Try
            conn.Open()

            adapter = New SqlDataAdapter
            adapter.SelectCommand = gradeComm
            adapter.Fill(dtGrade1)
           
            GridView1.DataSource = dtGrade1
            GridView1.DataBind()
               
        Catch ex As Exception

        Finally
            conn.Close()
        End Try
End Sub

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:BoundField DataField="STID" HeaderText="Student ID" />
        <asp:BoundField DataField="STFNAME" HeaderText="First Name" />
        <asp:BoundField DataField="STLNAME" HeaderText="Last Name" />
        <asp:BoundField DataField="CALVONEID" HeaderText="Calvert 1st Grade ID" />
    </Columns>
</asp:GridView>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
asp_net2Author Commented:
CALVERTGRADE2.ASCX:

<%@ Control Language="VB" ClassName="CalvertGrade1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.SqlTypes" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts.WebPart" %>

Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("PSSA").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connectionString)

        Dim dtGrade2 As DataTable = New DataTable()

        Dim grade1Comm As SqlCommand
        grade1Comm = New SqlCommand("WebPart2Test", conn)
        grade1Comm.CommandType = CommandType.StoredProcedure
       
        ' Create Parameter for Querystring
        grade1Comm.Parameters.Add("@stid", SqlDbType.Int)
        grade1Comm.Parameters("@stid").Value = Request("TextBox1.Text")
       
        Dim adapter As SqlDataAdapter

        Try
            conn.Open()

            adapter = New SqlDataAdapter
            adapter.SelectCommand = grade1Comm
            adapter.Fill(dtGrade2)
           
            GridView2.DataSource = dtGrade2
            GridView2.DataBind()
               
        Catch ex As Exception

        Finally
            conn.Close()
        End Try
End Sub

<asp:GridView ID="GridView2" runat="server">
    <Columns>
        <asp:BoundField DataField="STID" HeaderText="Student ID" />
        <asp:BoundField DataField="STFNAME" HeaderText="First Name" />
        <asp:BoundField DataField="STLNAME" HeaderText="Last Name" />
        <asp:BoundField DataField="CALVTWOID" HeaderText="Calvert 2nd Grade ID" />
    </Columns>
</asp:GridView>
0
 
asp_net2Author Commented:
crazyman,

Please note that i added as you will see on my UserControls Request("TextBox1.Text") and now i do not receive that error. HOWEVER, when i run my index.aspx page i have a button that needs to retrieve data from both UserControls through the ButtonClick Event on index.aspx but when i just run the page i receive the error message below.

'Button1_Click' is not a member of 'ASP.index_aspx'.
0
 
crazymanCommented:
This wont work

Request("TextBox1.Text")

However you could do

(this.Page.FindControl("TextBox1") as TextBox).Text
0
 
crazymanCommented:
Now i see.

You cannot have the Button1_Click events from the page fire the user control

Create a method on your user controls

public sub DoWork(text as string)
....#


then in your click on the page call

GridView2.DoWork(me.TextBox1.Text)
0
 
asp_net2Author Commented:
crazyman,

Ok, no ERRORS which is good but no DATA either :(

I think it has something to do with the following below and my lack of knowledge on how to use what you supplied.

' Create Parameter for Querystring
gradeComm.Parameters.Add("@stid", SqlDbType.Int)
gradeComm.Parameters("@stid").Value = Me.Page.FindControl("TextBox1") as TextBox).Text

i receive a message at --> as TextBox).Text  that says End of Statement Expected. Not sure what i need to do here.
0
 
asp_net2Author Commented:
Also how do i declare the GridView controls on the Button Click Event on index.aspx?

Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       
        -->  GridView1.DoWork1(Me.TextBox1.Text)
        -->  GridView2.DoWork2(Me.TextBox1.Text)

End Sub
0
 
asp_net2Author Commented:
Hi crazyman,

Are you still able to help with this?
0
 
crazymanCommented:
Okay
change this
gradeComm.Parameters("@stid").Value = Me.Page.FindControl("TextBox1") as TextBox).Text

to

gradeComm.Parameters("@stid").Value = text;
NB text is the name of your function param

0
 
asp_net2Author Commented:
Ok a few questions:

1.) I'm passing in an Integer value not String will this matter?
2.) When i did run everything after adding gradeComm.Parameters("@stid").Value = text; i got an error saying that i need to Declare GridView1 and GridView2 on the ButtonClick Event.
0
 
crazymanCommented:
1.Okay the integer value is fine provding your stored proc expects it.

2.You would need to do a FindControl to get the user control instances, this should be something along the lines of

CalvertGrade1 GridView1= this.WebPartZone1.FindControl("GridView1") as CalvertGrade1
CalvertGrade2 GridView2= this.WebPartZone1.FindControl("GridView2") as CalvertGrade2
0
 
asp_net2Author Commented:
crazyman,

I want you know that i have posted questions like this before and you will be my HERO if i get this to work since othes where unable to :)

You are correct with the Integer.

I added the last step below and do not receive any errors when running but when i run it now i don't get and data :( I'm pretty i have to do something with the functions that i created in my UserControl pages Public Sub DoWork1() but not sure how to tie this in to the following below.

WebPartGrade1 = Me.WebPartZone1.FindControl("GridView1")
WebPartGrade2 = Me.WebPartZone2.FindControl("GridView2")
0
 
crazymanCommented:
okay, providing you have AutoEventWireUp set to true, this in your index page should work...


Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
CalvertGrade1 GridView1= this.WebPartZone1.FindControl("GridView1") as CalvertGrade1 
CalvertGrade2 GridView2= this.WebPartZone1.FindControl("GridView2") as CalvertGrade2
 
GridView1.DoWork(cint(me.TextBox1.Text))
GridView2DoWork(cint(me.TextBox1.Text))
 
end sub

Open in new window

0
 
crazymanCommented:
Oops i see your vb.net

Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
dim GridView1 as CalvertGrade1 = DirectCast(this.WebPartZone1.FindControl("GridView1"), CalvertGrade1 )
dim GridView2 as CalvertGrade2 = DirectCast(this.WebPartZone1.FindControl("GridView2"), CalvertGrade2 )
 
GridView1.DoWork(cint(me.TextBox1.Text))
GridView2DoWork(cint(me.TextBox1.Text))
 
end sub

Open in new window

0
 
crazymanCommented:
Arggghhh, vb.net is not my forte

change the this for Me
0
 
asp_net2Author Commented:
It's okay i figured that i needed to change "this" to "Me".

Okay, i added all the code to my ButtonClick Event Handler and no errors/messages are appearing within the code, BUT when i run the page i receive the following error message below.

Object reference not set to an instance of an object.  

this message appears on the same line as --> GridView1.DoWork1(CInt(Me.TextBox1.Text))
0
 
crazymanCommented:
at a guess it could not find the GridView controls

first of change the second lline to look at WebPartZone2 not 1


then step through and hover over GridView1, is it null ?
0
 
asp_net2Author Commented:
>> first of change the second lline to look at WebPartZone2 not 1
I did change that back.

I'm using an ID that i used before and will work with both UserControls. I'm not sure what you mean by step through and hover over GridView1, to see if it's null.

would this be a problem see below with the word Text?

        gradeComm.Parameters.Add("@stid", SqlDbType.Int)
        gradeComm.Parameters("@stid").Value = Text    <------------
0
 
asp_net2Author Commented:
crazyman,

I also tried to Step over in Debug but that didn't work either i still get the same error message

Object reference not set to an instance of an object.
0
 
crazymanCommented:
can you report your code and i will take a look.

0
 
asp_net2Author Commented:
INDEX.ASPX PAGE:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.SqlTypes" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts.WebPart" %>
<%@ Register Src="~/CalvertGrade1.ascx" TagName="CalvertGrade1" TagPrefix="uc1" %>
<%@ Register Src="~/CalvertGrade2.ascx" TagName="CalvertGrade2" TagPrefix="uc2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode
    End Sub
   
    Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
       
        Dim GridView1 As CalvertGrade1 = DirectCast(Me.WebPartZone1.FindControl("GridView1"), CalvertGrade1)
        Dim GridView2 As CalvertGrade2 = DirectCast(Me.WebPartZone2.FindControl("GridView2"), CalvertGrade2)
 
        GridView1.DoWork1(CInt(Me.TextBox1.Text))
        GridView2.DoWork2(CInt(Me.TextBox1.Text))

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form2" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Find Data" OnClick="Button1_Click" />
    <br />
    <br />        

<asp:WebPartZone ID="WebPartZone1" runat="server" BorderColor="#CCCCCC" Font-Names="Verdana" Padding="6">
    <EmptyZoneTextStyle Font-Size="0.8em" />
    <PartStyle Font-Size="0.8em" ForeColor="#333333" />
    <TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />
    <MenuLabelHoverStyle ForeColor="Yellow" />
    <MenuPopupStyle BackColor="#1C5E55" BorderColor="#CCCCCC" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.6em" />
    <MenuVerbStyle BorderColor="#1C5E55" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />
    <PartTitleStyle BackColor="#1C5E55" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />

    <ZoneTemplate>
      <uc1:CalvertGrade1 ID="WebPartGrade1" runat="server"></uc1:CalvertGrade1>
    </ZoneTemplate>      

    <MenuVerbHoverStyle BackColor="#E3EAEB" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" ForeColor="#333333" />
    <PartChromeStyle BackColor="#E3EAEB" BorderColor="#C5BBAF" Font-Names="Verdana" ForeColor="#333333" />
    <HeaderStyle Font-Size="0.7em" ForeColor="#CCCCCC" HorizontalAlign="Center" />
    <MenuLabelStyle ForeColor="#333333" />
</asp:WebPartZone>  

<br />
<br />
     
<asp:WebPartZone ID="WebPartZone2" runat="server" BorderColor="#CCCCCC" Font-Names="Verdana" Padding="6">      
    <EmptyZoneTextStyle Font-Size="0.8em" />
    <PartStyle Font-Size="0.8em" ForeColor="#333333" />
    <TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />
    <MenuLabelHoverStyle ForeColor="#E2DED6" />
    <MenuPopupStyle BackColor="#5D7B9D" BorderColor="#CCCCCC" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.6em" />
    <MenuVerbStyle BorderColor="#5D7B9D" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />
    <PartTitleStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />

    <ZoneTemplate>
      <uc2:CalvertGrade2 ID="WebPartGrade2" runat="server"></uc2:CalvertGrade2>
    </ZoneTemplate>  

    <MenuVerbHoverStyle BackColor="#F7F6F3" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" ForeColor="#333333" />
    <PartChromeStyle BackColor="#F7F6F3" BorderColor="#E2DED6" Font-Names="Verdana" ForeColor="White" />
    <HeaderStyle Font-Size="0.7em" ForeColor="#CCCCCC" HorizontalAlign="Center" />
    <MenuLabelStyle ForeColor="White" />
</asp:WebPartZone>  
   
    </div>
    </form>
</body>
</html>
0
 
asp_net2Author Commented:
CALVERTGRADE1.ASCX PAGE:

<%@ Control Language="VB" ClassName="CalvertGrade1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.SqlTypes" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts.WebPart" %>

<script runat="server">
    Public Sub DoWork1(ByVal Text As Integer)
       
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("PSSA").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connectionString)

        Dim dtGrade1 As DataTable = New DataTable()

        Dim gradeComm As SqlCommand
        gradeComm = New SqlCommand("WebPart1Test", conn)
        gradeComm.CommandType = CommandType.StoredProcedure

        ' Create Parameter for Querystring
        gradeComm.Parameters.Add("@stid", SqlDbType.Int)
        gradeComm.Parameters("@stid").Value = Text
       
        Dim adapter As SqlDataAdapter

        Try
            conn.Open()

            adapter = New SqlDataAdapter
            adapter.SelectCommand = gradeComm
            adapter.Fill(dtGrade1)

            GridView1.DataSource = dtGrade1
            GridView1.DataBind()

Catch ex As Exception
           
        Finally
            conn.Close()
        End Try
    End Sub
</script>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="STID" HeaderText="Student ID" />
        <asp:BoundField DataField="STFNAME" HeaderText="First Name" />
        <asp:BoundField DataField="STLNAME" HeaderText="Last Name" />
        <asp:BoundField DataField="CALVONEID" HeaderText="Calvert 1st Grade ID" />
    </Columns>
</asp:GridView>
0
 
asp_net2Author Commented:
CALVERTGRADE2.ASCX PAGE:

<%@ Control Language="VB" ClassName="CalvertGrade2" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.SqlTypes" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts.WebPart" %>

<script runat="server">
    Public Sub DoWork2(ByVal Text As Integer)
       
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("PSSA").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connectionString)

        Dim dtGrade2 As DataTable = New DataTable()

        Dim grade1Comm As SqlCommand
        grade1Comm = New SqlCommand("WebPart2Test", conn)
        grade1Comm.CommandType = CommandType.StoredProcedure

        ' Create Parameter for Querystring
        grade1Comm.Parameters.Add("@stid", SqlDbType.Int)
        grade1Comm.Parameters("@stid").Value = Text
       
        Dim adapter As SqlDataAdapter

        Try
            conn.Open()

            adapter = New SqlDataAdapter
            adapter.SelectCommand = grade1Comm
            adapter.Fill(dtGrade2)

            GridView2.DataSource = dtGrade2
            GridView2.DataBind()
           
        Catch ex As Exception

        Finally
            conn.Close()
        End Try
    End Sub
</script>

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="STID" HeaderText="Student ID" />
        <asp:BoundField DataField="STFNAME" HeaderText="First Name" />
        <asp:BoundField DataField="STLNAME" HeaderText="Last Name" />
        <asp:BoundField DataField="CALVTWOID" HeaderText="Calvert 2nd Grade ID" />
    </Columns>
</asp:GridView>
0
 
crazymanCommented:
change
Dim GridView1 As CalvertGrade1 = DirectCast(Me.WebPartZone1.FindControl("WebPartGrade1"), CalvertGrade1)
        Dim GridView2 As CalvertGrade2 = DirectCast(Me.WebPartZone2.FindControl("WebPartGrade2"), CalvertGrade2)
0
 
asp_net2Author Commented:
I recieve the following error message below.
Error message: Object reference not set to an instance of an object.

The error is on the line below.
GridView1.DoWork1(CInt(Me.TextBox1.Text))
0
 
asp_net2Author Commented:
Hi crazyman,

I also tried different settings for what you provided in 22740248 but it still goes to GridView1.DoWork1(CInt(Me.TextBox1.Text)) for the error.
0
 
asp_net2Author Commented:
Please let me know if you are still able to help with this post. Thank you!
0
 
asp_net2Author Commented:
Hi crazyman i found someone else to help me finish this post since i didn't hear back from you. So i figured i would show you what was needed. See below.

Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
       
        Dim WebPartGrade1 As WebPart = WebPartManager1.WebParts("WebPartGrade1")
        Dim WebPartGrade2 As WebPart = WebPartManager1.WebParts("WebPartGrade2")
       
        Dim GridView1 As CalvertGrade1 = DirectCast(WebPartGrade1.Controls(0), CalvertGrade1)
        Dim GridView2 As CalvertGrade2 = DirectCast(WebPartGrade2.Controls(0), CalvertGrade2)
 
        If Me.TextBox1.Text = "" Then
            TextBox1.Text = ""
        Else
            GridView1.DoWork1(CInt(Me.TextBox1.Text))
            GridView2.DoWork2(CInt(Me.TextBox1.Text))
        End If
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 19
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now