Retrieving data from Custom Control
Hi
I have created a custom user Control (with 5 text boxes) for a job site to accept N Number of Work Experience.
I Load the first Custom control by default on load with a command button to add more Experience. Every time I click the "Add More" button the Custom Control loads on the browser.
My problem is I don't know how to to extract the data from each of the input boxes of each of the control.
Can anyone help me how to do this?
Here are the detail of the files:
Custom Control File: UserControlWorkExperience.asCx
Interface File:UserControlEducation.asPx
Code of UserControlEducation.ASCX
Code:
<script runat="server">
Public Property CompanyName() As String
Get
Return txtCompanyName.Text
End Get
Set(ByVal Value As String)
txtCompanyName.Text = Value
End Set
End Property
Public Property MOJ() As String
Get
Return txtMOJ.Text
End Get
Set(ByVal Value As String)
txtMOJ.Text = Value
End Set
End Property
Public Property YOJ() As String
Get
Return txtYOJ.Text
End Get
Set(ByVal Value As String)
txtYOJ.Text = Value
End Set
End Property
Public Property MOL() As String
Get
Return txtMOL.Text
End Get
Set(ByVal Value As String)
txtMOL.Text = Value
End Set
End Property
Public Property YOL() As String
Get
Return txtYOL.Text
End Get
Set(ByVal Value As String)
txtYOL.Text = Value
End Set
End Property
Public Property JobTitle() As String
Get
Return txtJobTitle.Text
End Get
Set(ByVal Value As String)
txtJobTitle.Text = Value
End Set
End Property
Public Property JobDetail() As String
Get
Return txtJobDetail.Text
End Get
Set(ByVal Value As String)
txtJobDetail.Text = Value
End Set
End Property
</script>
<table width="100%" border="0">
<tr>
<td width="26%" class="FormFieldTitle">Company Name<br> <asp:textbox Height="18" Font-Size="10" ID="txtCompanyName" MaxLength="150" runat="server" Width="200" />
</td>
<td width="26%">Month of Join<br> <asp:textbox Height="18" Font-Size="10" ID="txtMOJ" MaxLength="15" runat="server" Width="30" />
</td>
<td width="13%">Year of Join<br> <asp:textbox Height="18" Font-Size="10" ID="txtYOJ" MaxLength="50" runat="server" Width="30" />
</td>
<td width="17%">Month of Leave<br> <asp:textbox Font-Size="10" Height="18" ID="txtMOL" MaxLength="4" runat="server" Width="30" />
</td>
<td width="18%">Year of Leave<br> <asp:textbox Font-Size="10" Height="15" ID="txtYOL" MaxLength="4" runat="server" Width="30" /></td>
</tr>
<tr>
<td colspan="2">Job Title<br> <asp:textbox Font-Size="10" Height="18" ID="txtJobTitle" MaxLength="30" runat="server" Width="350" />
</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2">Job Detail<br> <asp:textbox Font-Size="10" ID="txtJobDetail" runat="server" Width="450" TextMode="MultiLine" Rows="10" /> </td>
<td> </td>
<td> </td>
<td>
</td>
</tr>
</table>
***********************
Code:
Code of UserControlWorkExperience.aspx
<%@ Register TagPrefix="UserWorkExperience" TagName="WorkExperience" Src="UserControlWorkExperience.ascx" %>
<script runat="server">
Sub Page_Load()
Dim intCounterWorkExperience as Integer
If Not IsPostBack then
ViewState("WorkExperienceCtlCount")=1
ElseIf ViewState("WorkExperienceCtlCount")>1 Then
For intCounterWorkExperience = 2 to ViewState("WorkExperienceCtlCount")
AddWorkExperienceControl(intCounterWorkExperience)
Next
End If
End Sub
Sub AddControlButton_Click(s as Object, e as EventArgs)
ViewState("WorkExperienceCtlCount") +=1
AddWorkExperienceControl(ViewState("WorkExperienceCtlCount"))
End Sub
Sub AddWorkExperienceControl(strCtlNum as String)
Dim ctlWorkExperience as Control
ctlWorkExperience=LoadControl("UserControlWorkExperience.ascx")
ctlWorkExperience.ID="WorkExperience" & strCtlNum
plhWorkExperience.Controls.Add(ctlWorkExperience)
End Sub
Sub Show_Detail(s as Object, e as Eventargs)
response.write(plhWorkExperience.WorkExperience1)
End Sub
</script>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="Styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<form runat="server">
<asp:placeholder id="plhWorkExperience" Runat="Server" />
<asp:button OnClick="AddControlButton_Click" text="Add More" runat="server" />
<asp:button OnClick="Show_Detail" text="Show Detail" runat="server" />
</form>
</body>
</html>
*********
Re: Retrieving data from Custom Control
I see that you've declared the public properties in each of your controls. You should be assigning the values in the input boxes to these public variables. You can then access those values by using something like
MyControl1.MonthOfJoining
Now, if you want to iterate through each of them without knowing in advance how many you have on the page, you should loop through all of the controls in the Page object.
VB Code:
For Each ctl As Control in Page.Controls
If TypeOf ctl Is MyControlType Then
'You can access the info using
CType(ctl,MyControlType).MonthOfJoining
End If
Next
HTH