lnshop
asked on
How to run an ascx user control from an html control
I have an html dropdown on my aspx page. I need to populate the dropdown from a database. As I will need to run this same code on many web forms, I want to just drop the control on the form. The control contains the database connection, query and writes the output to populate the dropdown. The code is VB.NET using a odbc dataadapter, odbc connection and dataset.
When I run the page, the code runs at the top of page and not from within the html control. Is there a way to run the user control from the html control. Any assistance would be appreciated. Shown below is what the html looks like.
<SELECT id="Employee" tabIndex="11" name="Employee">
<option value="None" selected>None</OPTION>
<uc2:ClaimsEmployeesContro l id="ClaimsEmployeesControl 1" runat="server"></uc2:Claim sEmployees Control>
</Select>
I also tried calling the sub name and this did not work.
<option value="None" selected>None</OPTION>
<% showoption() %>
</Select>
Thank you very much for your assistance.
When I run the page, the code runs at the top of page and not from within the html control. Is there a way to run the user control from the html control. Any assistance would be appreciated. Shown below is what the html looks like.
<SELECT id="Employee" tabIndex="11" name="Employee">
<option value="None" selected>None</OPTION>
<uc2:ClaimsEmployeesContro
</Select>
I also tried calling the sub name and this did not work.
<option value="None" selected>None</OPTION>
<% showoption() %>
</Select>
Thank you very much for your assistance.
Nope, HTML Select control does not allow tags to nested inside the select tags, excpets for option tags. You will need to make select drop down part of your control.
ASKER
Thank you for your assistance. I tried adding the starting <Select and ending select tags> to the control. The entire dropdown is now being generated from the vb.net code but the dropdown is again written at the top of the page and not in the table <td>. The html code now looks like
<td noWrap align="left">
<uc2:ClaimsEmployeesContro l id="ClaimsEmployeesControl 1" runat="server"></uc2:Claim sEmployees Control></ td>
<td noWrap align="left">
<uc2:ClaimsEmployeesContro
Can you post the UC markup?
ASKER
Here is the vb.net code from the code behind page. Thank-you.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
oconnClms.Open()
showoption()
oconnClms.Close()
End Sub
Sub showoption()
Response.Write("<SELECT id=""Employee"" tabIndex=""11"" name=""Employee"">")
Response.Write("<OPTION value=""None"" selected>None</OPTION>")
DAClms.Fill(DsClms1)
Dim i As Integer
Dim firstnam As String
firstnam = Trim(firstnam)
Dim midname As String
midname = Trim(midname)
Dim lastname As String
lastname = Trim(lastname)
Dim designat As String
designat = Trim(designat)
Dim job_title As String
job_title = Trim(job_title)
Dim department As String
department = Trim(department)
For i = 0 To DsClms1.Tables(0).Rows.Cou nt - 1
firstnam = Trim(DsClms1.Tables(0).Row s(i).Item( 0).ToStrin g())
midname = Trim(DsClms1.Tables(0).Row s(i).Item( 1).ToStrin g())
lastname = Trim(DsClms1.Tables(0).Row s(i).Item( 2).ToStrin g())
designat = Trim(DsClms1.Tables(0).Row s(i).Item( 3).ToStrin g())
job_title = Trim(DsClms1.Tables(0).Row s(i).Item( 4).ToStrin g())
department = Trim(DsClms1.Tables(0).Row s(i).Item( 5).ToStrin g())
If designat <> "" Then
lastname = lastname & ","
designat = designat & ","
'job_title = job_title & ","
Else
lastname = lastname & ","
job_title = job_title & ","
End If
Response.Write("<option value=""" & Trim(firstnam) & " " & Trim(midname) & " " & Trim(lastname) & " " & Trim(designat) & Trim(job_title) & """")
Response.Write(">" & Trim(firstnam) & " " & Trim(midname) & " " & Trim(lastname) & " " & Trim(designat) & Trim(job_title) & "</option>" & vbCrLf)
Next
Response.Write("</Select>" )
End Sub
End Class
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
oconnClms.Open()
showoption()
oconnClms.Close()
End Sub
Sub showoption()
Response.Write("<SELECT id=""Employee"" tabIndex=""11"" name=""Employee"">")
Response.Write("<OPTION value=""None"" selected>None</OPTION>")
DAClms.Fill(DsClms1)
Dim i As Integer
Dim firstnam As String
firstnam = Trim(firstnam)
Dim midname As String
midname = Trim(midname)
Dim lastname As String
lastname = Trim(lastname)
Dim designat As String
designat = Trim(designat)
Dim job_title As String
job_title = Trim(job_title)
Dim department As String
department = Trim(department)
For i = 0 To DsClms1.Tables(0).Rows.Cou
firstnam = Trim(DsClms1.Tables(0).Row
midname = Trim(DsClms1.Tables(0).Row
lastname = Trim(DsClms1.Tables(0).Row
designat = Trim(DsClms1.Tables(0).Row
job_title = Trim(DsClms1.Tables(0).Row
department = Trim(DsClms1.Tables(0).Row
If designat <> "" Then
lastname = lastname & ","
designat = designat & ","
'job_title = job_title & ","
Else
lastname = lastname & ","
job_title = job_title & ","
End If
Response.Write("<option value=""" & Trim(firstnam) & " " & Trim(midname) & " " & Trim(lastname) & " " & Trim(designat) & Trim(job_title) & """")
Response.Write(">" & Trim(firstnam) & " " & Trim(midname) & " " & Trim(lastname) & " " & Trim(designat) & Trim(job_title) & "</option>" & vbCrLf)
Next
Response.Write("</Select>"
End Sub
End Class
When HTML streams out at the top of the page usually means that there is some malformed HTML, an ortphan tag or something. Can you view the source of the page, and see if you control is streaming an extra quote or something?
ASKER
I had a look at the source and could not see anything ut of the ordinary.
The data coming from the database is not cleaned up and parsed until the drop down's on_click event is fired. If I moved the code from the vbcode behind to the html server side and called the sub using <% showoption() %>, would this make a difference. I don't see that there would be a problem recognzing the call but that is not the solution I wanted. I would prefer to keep the code behind. This is legacy asp code I am converting to .net and much prefer the visuals of the connection, adapter and dataset.
Thank-you.
The data coming from the database is not cleaned up and parsed until the drop down's on_click event is fired. If I moved the code from the vbcode behind to the html server side and called the sub using <% showoption() %>, would this make a difference. I don't see that there would be a problem recognzing the call but that is not the solution I wanted. I would prefer to keep the code behind. This is legacy asp code I am converting to .net and much prefer the visuals of the connection, adapter and dataset.
Thank-you.
Can you post aspx page? And are you sure that your data doesn't have single quotes, like "O'Brian" ?
If you want to do this in a true server-side manner, you should use a .net dropdown list control, not the HTML drop-down. That way you can take advantage of view state and postback events.
Instead of hand-coding the HTML, drop a server dropdown control on the page and add items to its Items collection:
DropDownList1.Items.Add(Tr im(firstna m) & " " & Trim(midname) & " " & Trim(lastname) & " " & Trim(designat) & Trim(job_title))
If you want to do this in a true server-side manner, you should use a .net dropdown list control, not the HTML drop-down. That way you can take advantage of view state and postback events.
Instead of hand-coding the HTML, drop a server dropdown control on the page and add items to its Items collection:
DropDownList1.Items.Add(Tr
ASKER
Here is the html code for the aspx page. All validation and vbs functions are locaterd in the CTheme control and this works very well. Viewstate and postback are not required for this but likely will be for another project. I will try the server drop down control. Thank-you.
<%@ Page Language="vb" CodeBehind="ClaimsCICALtrC ertofInsur UW.aspx.vb " AutoEventWireup="false" Inherits="ConvertedAdminFo lder2.Clai msCICALtrC ertofInsur UW" EnableSessionState="False" enableViewState="False"%>
<%@ Register TagPrefix="uc1" TagName="CTheme" Src="CTheme.ascx" %>
<%@ Register TagPrefix="uc2" TagName="CEmployeesControl " Src="CEmployeesControl.asc x" %>
<HTML>
<HEAD>
<meta content="VBScript" name="vs_defaultClientScri pt">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="ClaimsStyleSheet.css " type="text/css" rel="stylesheet">
</HEAD>
<body>
<form name="FormName" action="../word/transit.as px" method="get">
<input type="hidden" value="\\webserver\test.do c" name="main">
<table class="outertable" style="WIDTH: 392px; HEIGHT: 414px" cellSpacing="0" cellPadding="0"
align="center">
<tr>
<td>
<table class="innertable" style="WIDTH: 392px; HEIGHT: 414px" cellSpacing="2" cellPadding="2"
align="center" border="0">
<tr>
<td class="FormHeading" colSpan="2">Claims Division - CICA Certificate of Insurance
Letter</td>
</tr>
<tr>
<td align="right">Company</td>
<td noWrap><input tabIndex="1" size="40" name="Company">
<script language="vbscript">
document.FormName.Company. focus
</script>
</td>
</tr>
<tr>
<td noWrap align="right">Address</td>
<td noWrap><input tabIndex="2" size="40" name="Address1"></td>
</tr>
<tr>
<td noWrap align="right">Address</td>
<td noWrap><input tabIndex="3" size="40" name="Address2"></td>
</tr>
<tr>
<td noWrap align="right">Address</td>
<td noWrap><input tabIndex="4" size="40" name="Address3"></td>
</tr>
<tr>
<td noWrap align="right">Attention</t d>
<td noWrap><input tabIndex="5" size="40" name="Attention"></td>
</tr>
<tr>
<td noWrap align="right">Insured</td>
<td noWrap><input style="TEXT-TRANSFORM: uppercase" tabIndex="6" size="40" name="INSURED"></td>
</tr>
<tr>
<td noWrap align="right">Loss Date</td>
<td noWrap><input tabIndex="7" onchange="checkdateMDY(me) " size="40" name="DateofLoss"></td>
</tr>
<tr>
<td noWrap align="right">Your File</td>
<td noWrap><input tabIndex="8" size="40" name="YourFile"></td>
</tr>
<tr>
<td noWrap align="right">Our File</td>
<td noWrap><input tabIndex="9" size="40" name="OurFile"></td>
</tr>
<TR>
<TD noWrap align="right">Loss Location</TD>
<TD noWrap><input tabIndex="10" size="40" name="LossLocation"></TD>
</TR>
<tr>
<td noWrap align="right">Signature</t d>
<td noWrap align="left">
<uc2:ClaimsEmployeesContro l id="ClaimsEmployeesControl 1" runat="server"></uc2:Claim sEmployees Control>
</td>
<tr>
<td noWrap align="right">Name</td>
<td noWrap align="left"><input tabIndex="12" size="40" name="name">
</td>
</tr>
<tr>
<td noWrap align="right">Title</td>
<td noWrap align="left"><input tabIndex="13" size="40" name="JobTitle">
</td>
</tr>
<tr>
<td noWrap align="right">Department</ td>
<td noWrap align="left"><input tabIndex="14" size="40" name="Department">
</td>
</tr>
<tr>
<td noWrap align="right">Document Id</td>
<td noWrap align="left"><input tabIndex="15" size="40" name="DocumentId">
</td>
</tr>
<tr>
<td align="center" colSpan="2"><input style="WIDTH: 55px" tabIndex="20" type="button" value="Claims" name="ClmsHome">
<input tabIndex="19" type="button" value="Cancel" name="Cancel"> <input tabIndex="17" type="submit" value="Submit" name="Submit">
<input tabIndex="18" type="reset" value="Reset" name="Reset"> <A class="A" href="#top">
Back to Top</A></td>
</tr>
</table>
</td>
</tr>
</table>
<uc1:claimstheme id="ClaimsTheme1" runat="server"></uc1:claim stheme></f orm>
</body>
</HTML>
<%@ Page Language="vb" CodeBehind="ClaimsCICALtrC
<%@ Register TagPrefix="uc1" TagName="CTheme" Src="CTheme.ascx" %>
<%@ Register TagPrefix="uc2" TagName="CEmployeesControl
<HTML>
<HEAD>
<meta content="VBScript" name="vs_defaultClientScri
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="ClaimsStyleSheet.css
</HEAD>
<body>
<form name="FormName" action="../word/transit.as
<input type="hidden" value="\\webserver\test.do
<table class="outertable" style="WIDTH: 392px; HEIGHT: 414px" cellSpacing="0" cellPadding="0"
align="center">
<tr>
<td>
<table class="innertable" style="WIDTH: 392px; HEIGHT: 414px" cellSpacing="2" cellPadding="2"
align="center" border="0">
<tr>
<td class="FormHeading" colSpan="2">Claims Division - CICA Certificate of Insurance
Letter</td>
</tr>
<tr>
<td align="right">Company</td>
<td noWrap><input tabIndex="1" size="40" name="Company">
<script language="vbscript">
document.FormName.Company.
</script>
</td>
</tr>
<tr>
<td noWrap align="right">Address</td>
<td noWrap><input tabIndex="2" size="40" name="Address1"></td>
</tr>
<tr>
<td noWrap align="right">Address</td>
<td noWrap><input tabIndex="3" size="40" name="Address2"></td>
</tr>
<tr>
<td noWrap align="right">Address</td>
<td noWrap><input tabIndex="4" size="40" name="Address3"></td>
</tr>
<tr>
<td noWrap align="right">Attention</t
<td noWrap><input tabIndex="5" size="40" name="Attention"></td>
</tr>
<tr>
<td noWrap align="right">Insured</td>
<td noWrap><input style="TEXT-TRANSFORM: uppercase" tabIndex="6" size="40" name="INSURED"></td>
</tr>
<tr>
<td noWrap align="right">Loss Date</td>
<td noWrap><input tabIndex="7" onchange="checkdateMDY(me)
</tr>
<tr>
<td noWrap align="right">Your File</td>
<td noWrap><input tabIndex="8" size="40" name="YourFile"></td>
</tr>
<tr>
<td noWrap align="right">Our File</td>
<td noWrap><input tabIndex="9" size="40" name="OurFile"></td>
</tr>
<TR>
<TD noWrap align="right">Loss Location</TD>
<TD noWrap><input tabIndex="10" size="40" name="LossLocation"></TD>
</TR>
<tr>
<td noWrap align="right">Signature</t
<td noWrap align="left">
<uc2:ClaimsEmployeesContro
</td>
<tr>
<td noWrap align="right">Name</td>
<td noWrap align="left"><input tabIndex="12" size="40" name="name">
</td>
</tr>
<tr>
<td noWrap align="right">Title</td>
<td noWrap align="left"><input tabIndex="13" size="40" name="JobTitle">
</td>
</tr>
<tr>
<td noWrap align="right">Department</
<td noWrap align="left"><input tabIndex="14" size="40" name="Department">
</td>
</tr>
<tr>
<td noWrap align="right">Document Id</td>
<td noWrap align="left"><input tabIndex="15" size="40" name="DocumentId">
</td>
</tr>
<tr>
<td align="center" colSpan="2"><input style="WIDTH: 55px" tabIndex="20" type="button" value="Claims" name="ClmsHome">
<input tabIndex="19" type="button" value="Cancel" name="Cancel"> <input tabIndex="17" type="submit" value="Submit" name="Submit">
<input tabIndex="18" type="reset" value="Reset" name="Reset"> <A class="A" href="#top">
Back to Top</A></td>
</tr>
</table>
</td>
</tr>
</table>
<uc1:claimstheme id="ClaimsTheme1" runat="server"></uc1:claim
</body>
</HTML>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank-you very much. I will give that a try tomorrow morning.
ASKER
Thank you very much for your help. I finally got this working today. Below is the code.
oconnClms.Open()
Dim mycontrol As String
mycontrol = "<SELECT id=""Employee"" tabIndex=""11"" name=""Employee"" class=""Employee"" onclick=""doEmployee()"">"
mycontrol = mycontrol & "<OPTION value=""None"" selected>None</OPTION>"
Literal1.Text = mycontrol
DAClms.Fill(DsClms1)
Dim i As Integer
Dim firstnam As String
firstnam = Trim(firstnam)
Dim midname As String
midname = Trim(midname)
Dim lastname As String
lastname = Trim(lastname)
Dim designat As String
designat = Trim(designat)
Dim job_title As String
job_title = Trim(job_title)
Dim department As String
department = Trim(department)
For i = 0 To DsClms1.Tables(0).Rows.Cou nt - 1
firstnam = Trim(DsClms1.Tables(0).Row s(i).Item( 0).ToStrin g())
midname = Trim(DsClms1.Tables(0).Row s(i).Item( 1).ToStrin g())
lastname = Trim(DsClms1.Tables(0).Row s(i).Item( 2).ToStrin g())
designat = Trim(DsClms1.Tables(0).Row s(i).Item( 3).ToStrin g())
job_title = Trim(DsClms1.Tables(0).Row s(i).Item( 4).ToStrin g())
department = Trim(DsClms1.Tables(0).Row s(i).Item( 5).ToStrin g())
If designat <> "" Then
lastname = lastname & ","
designat = designat & ","
Else
lastname = lastname & ","
End If
Dim name As String
name = "<option value=""" & Trim(firstnam) & " " & Trim(midname) & Trim(lastname) & " " & Trim(designat) & Trim(job_title) & """"
name = name & ">" & Trim(firstnam) & " " & Trim(midname) & Trim(lastname) & " " & Trim(designat) & Trim(job_title) & "</option>" & vbCrLf
Literal1.Text = Literal1.Text & name
Next
Literal1.Text = Literal1.Text & "</Select>"
oconnClms.Close()
End Sub
oconnClms.Open()
Dim mycontrol As String
mycontrol = "<SELECT id=""Employee"" tabIndex=""11"" name=""Employee"" class=""Employee"" onclick=""doEmployee()"">"
mycontrol = mycontrol & "<OPTION value=""None"" selected>None</OPTION>"
Literal1.Text = mycontrol
DAClms.Fill(DsClms1)
Dim i As Integer
Dim firstnam As String
firstnam = Trim(firstnam)
Dim midname As String
midname = Trim(midname)
Dim lastname As String
lastname = Trim(lastname)
Dim designat As String
designat = Trim(designat)
Dim job_title As String
job_title = Trim(job_title)
Dim department As String
department = Trim(department)
For i = 0 To DsClms1.Tables(0).Rows.Cou
firstnam = Trim(DsClms1.Tables(0).Row
midname = Trim(DsClms1.Tables(0).Row
lastname = Trim(DsClms1.Tables(0).Row
designat = Trim(DsClms1.Tables(0).Row
job_title = Trim(DsClms1.Tables(0).Row
department = Trim(DsClms1.Tables(0).Row
If designat <> "" Then
lastname = lastname & ","
designat = designat & ","
Else
lastname = lastname & ","
End If
Dim name As String
name = "<option value=""" & Trim(firstnam) & " " & Trim(midname) & Trim(lastname) & " " & Trim(designat) & Trim(job_title) & """"
name = name & ">" & Trim(firstnam) & " " & Trim(midname) & Trim(lastname) & " " & Trim(designat) & Trim(job_title) & "</option>" & vbCrLf
Literal1.Text = Literal1.Text & name
Next
Literal1.Text = Literal1.Text & "</Select>"
oconnClms.Close()
End Sub