Dynamic Controls

R

rn5a

Assume that a ASPX page uses a user control named Address.ascx which
has 2 TextBoxes. This ASCX page creates 2 properties named 'Address' &
'City' using the Get & Set statements:

<script runat="server">
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property

Public Property City() As String
Get
City = txtCity.Text
End Get
Set(ByVal value As String)
txtCity.Text = value
End Set
End Property
</script>

Address: <asp:TextBox ID="txtAddress" runat="server"/>
City: <asp:TextBox ID="txtCity" runat="server"/>

This function in a VB class file takes UserID as a parameter & returns
a SqlDataReader to the calling function which exists in a ASPX page:

Namespace NConnect
Public Class Cart
Private sqlConn As New SqlConnection(".....")
Public Function GetAddress(ByVal UserID As Integer) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader

sqlCmd = New SqlCommand("NETGetAddress", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure

Try
With sqlCmd
.Parameters.Add("@UserID", SqlDbType.Int).Value =
UserID
End With

sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader
Catch ex As Exception
Throw ex
End Try

Return sqlReader
End Function
End Class
End Namespace

Using vbc, I successfully compiled the above class file into a DLL
named NConnect.dll.

This is the ASPX page that uses the user control & the DLL:

<%@ Register TagPrefix="NETConnect" TagName="Address"
Src="Address.ascx" %>
<%@ Import Namespace="NConnect" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
Public billAddress As address_ascx
Public shipAddress As address_ascx

Sub Page_Load(......)
Dim sqlReader As SqlDataReader
Dim iUserID As Integer
Dim boCart As Cart

boCart = New Cart

billAddress = Page.LoadControl("Address.ascx")
billAddress.ID = "ncBilling"

shipAddress = Page.LoadControl("Address.ascx")
shipAddress.ID = "ncShipping"

'retrieving UserID by calling a function

sqlReader = boCart.GetAddress(iUserID)

While(sqlReader.Read)
billAddress.Address = sqlReader.GetString(1)
billAddress.City = sqlReader.GetString(2)

shipAddress.Address = sqlReader.GetString(1)
shipAddress.City = sqlReader.GetString(2)
End While

pnlBillAddress.Controls.Add(billAddress)
pnlShipAddress.Controls.Add(shipAddress)
End Sub

Sub Submit_Click(obj As Object, ea As EventArgs)
.............
'how do I retrieve the data a user
'has entered in the 4 TextBoxes?
End Sub
</script>

<form runat="server">
<asp:panel ID="pnlBillAddress" runat="server"/><br>
<asp:panel ID="pnlShipAddress" runat="server"/><br>
<asp:Button ID="btn" OnClick="Submit_Click" Text="Submit"
runat="server"/>
</form>

The ASPX page displays 2 Panels with each Panel displaying 2 TextBoxes.
Now since the 4 TextBoxes are being added to the ASPX page dynamically,
how do I find out what is the ID of each of the 4 TextBoxes so that
Submit_Click sub can retrieve the data that has been entered by a user
in the 4 TextBoxes?
 
C

Cowboy \(Gregory A. Beamer\)

Submit can ONLY retrieve values from the properties on the two controls, as
that is the only exposure. Okay, not quite true, but it is the sane way to
do it.

In submit:

Dim address As String = billAddress.Address
Dim city As STring = billAddress.City

Dim address2 As String = shipAddress.Address
Dim city2 As STring = shipAddress.City

Think of your control as a person you are asking questions of rather than a
cadaevor you are slicing open to pull out its organs. :)
 
B

bruce barker \(sqlwork.com\)

couple steps.

1) the user control should implment an inteface to access the text boxes
2) you then cast the user control to the iterface and can access the values
from the page.
3) for the user control to get its postback data, it must be created in
oninit not onload.


-- bruce (sqlwork.com)
 
R

rn5a

Gregory, I should have mentioned an important point in my original post
which I completely forgot about.

Actually the ASPX page uses a RadioButtonList with 2 ListItems - Yes &
No.

When this page loads for the first time, neither of the 2 RadioButtons
are checked by default & hence apart from these 2 RadioButtons, the
Panels along with the user controls aren't displayed.

If the user checks the Yes RadioButton, the Address & City TextBoxes in
the 2 Panels get displayed & at the same time, all the 4 TextBoxes get
populated with records from the DB table. That's exactly what the ASPX
code in my first post does - only difference being it shouldn't be in
the Page_Load sub; rather it should be in the RadioButtonList's
OnSelectedIndexChanged event handler (which is named 'ShowHidePanel'
shown below).

But if the user checks the No RadioButton, I create the TextBoxes like
this in the ASPX page:

<asp:panel ID="pnlAddAddress" runat="server">
<NetConnect:Address ID="ncBilling1" runat="server"/><br>
<NetConnect:Address ID="ncShipping1" runat="server"/><br>
</asp:panel>

& this is the RadioButtonList:

Use Existing Address?<br>
<asp:RadioButtonList ID="rdlAddress"
OnSelectedIndexChanged="ShowHidePanel" AutoPostBack="true"
runat="server">
<asp:ListItem Value="yes">Yes</asp:ListItem>
<asp:ListItem Value="no">No</asp:ListItem>
</asp:RadioButtonList>

The ShowHidePanel sub hides/unhides the Panels accordingly. Now
assuming that the Yes RadioButton is checked (i.e. the 4 TextBoxes are
already populated with records from the DB), when I click the Button
using the following code:

Sub Submit_Click(obj As Object, ea As EventArgs)
If (rdlAddress.SelectedItem.Value = "yes") Then
Dim addr As String

addr = billAddress.Address
Response.Write("Address: " & addr)
End If
End Sub

this error gets generated:

Object reference not set to an instance of an object.

pointing to the

addr = billAddress.Address

line. How do I overcome this error?
 
R

rn5a

Gregory, assuming that the Yes RadioButton is checked, if I dynamically
re-create the user-controls in the Submit Button Click event (just
copy-paste the ASPX code from my first post in the Submit_Click sub)
like this:

Sub Submit_Click(......)
If (rdlAddress.SelectedItem.Value = "yes") Then
Dim sqlReader As SqlDataReader
Dim iUserID As Integer
Dim boCart As Cart

boCart = New Cart

billAddress = Page.LoadControl("Address.ascx")
billAddress.ID = "ncBilling"

shipAddress = Page.LoadControl("Address.ascx")
shipAddress.ID = "ncShipping"

'retrieving UserID by calling a function
sqlReader = boCart.GetAddress(iUserID)

While(sqlReader.Read)
billAddress.Address = sqlReader.GetString(1)
billAddress.City = sqlReader.GetString(2)

shipAddress.Address = sqlReader.GetString(1)
shipAddress.City = sqlReader.GetString(2)
End While

pnlBillAddress.Controls.Add(billAddress)
pnlShipAddress.Controls.Add(shipAddress)

Dim strBillAddress As String

strBillAddress = billAddress.Address
Response.Write("Billing Address: " & strBillAddress)
End If
End Sub

then the above code works fine. So does that mean that USER CONTROLS
NEED TO BE RE-CREATED DYNAMICALLY AFTER EACH & EVERY POSTBACK?

Atleast that's what I could conclude. Correct me if I am wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top