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:ClaimsEmployeesControl id="ClaimsEmployeesControl1" runat="server"></uc2:ClaimsEmployeesControl>
</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.
lnshopAsked:
Who is Participating?
 
kraffayCommented:
That all looks good, and I think the only reason your dropdown is showing up at the top of the page is because asp.net first streams out the response from the code behind, then renders the rest of the page.  

Try using a literal control in place of the user control.  

Instead of this:

<uc2:ClaimsEmployeesControl id="ClaimsEmployeesControl1" runat="server"></uc2:ClaimsEmployeesControl>

Do this:
<asp:literal id="litMyDropDown" runat="server" />

Then,  in the code behind, build a string (ideally a stringbuild object) representing your dropdown, and assing that string to the literal control's text property:

Dim myHTML as String

myHTML = ("<SELECT id=""Employee"" tabIndex=""11"" name=""Employee"">")
myHTML &= yada...yada..
myHTML &= "</Select>"

litMyDropDown.Text = myHTML

**********************************

That should get your dropdown rendered in the correct place.


0
 
kraffayCommented:
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.
0
 
lnshopAuthor Commented:
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:ClaimsEmployeesControl id="ClaimsEmployeesControl1" runat="server"></uc2:ClaimsEmployeesControl></td>



0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
kraffayCommented:
Can you post the UC markup?
0
 
lnshopAuthor Commented:
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.Count - 1
            firstnam = Trim(DsClms1.Tables(0).Rows(i).Item(0).ToString())
            midname = Trim(DsClms1.Tables(0).Rows(i).Item(1).ToString())
            lastname = Trim(DsClms1.Tables(0).Rows(i).Item(2).ToString())
            designat = Trim(DsClms1.Tables(0).Rows(i).Item(3).ToString())
            job_title = Trim(DsClms1.Tables(0).Rows(i).Item(4).ToString())
            department = Trim(DsClms1.Tables(0).Rows(i).Item(5).ToString())

            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
0
 
kraffayCommented:
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?
0
 
lnshopAuthor Commented:
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.
0
 
kraffayCommented:
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(Trim(firstnam) & " " & Trim(midname) & " " & Trim(lastname) & " " & Trim(designat) & Trim(job_title))
0
 
lnshopAuthor Commented:
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="ClaimsCICALtrCertofInsurUW.aspx.vb" AutoEventWireup="false" Inherits="ConvertedAdminFolder2.ClaimsCICALtrCertofInsurUW" EnableSessionState="False" enableViewState="False"%>
<%@ Register TagPrefix="uc1" TagName="CTheme" Src="CTheme.ascx" %>
<%@ Register TagPrefix="uc2" TagName="CEmployeesControl" Src="CEmployeesControl.ascx" %>
<HTML>
<HEAD>
<meta content="VBScript" name="vs_defaultClientScript">
<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.aspx" method="get">
                  <input type="hidden" value="\\webserver\test.doc" 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</td>
                                                <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</td>
                                                <td noWrap align="left">
                                                      <uc2:ClaimsEmployeesControl id="ClaimsEmployeesControl1" runat="server"></uc2:ClaimsEmployeesControl>
                                                </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">&nbsp;<A class="A" href="#top">
                                                            Back to Top</A></td>
                                          </tr>
                                    </table>
                              </td>
                        </tr>
                  </table>
                  <uc1:claimstheme id="ClaimsTheme1" runat="server"></uc1:claimstheme></form>
      </body>
</HTML>
0
 
lnshopAuthor Commented:
Thank-you very much.  I will give that a try tomorrow morning.
0
 
lnshopAuthor Commented:
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.Count - 1
            firstnam = Trim(DsClms1.Tables(0).Rows(i).Item(0).ToString())
            midname = Trim(DsClms1.Tables(0).Rows(i).Item(1).ToString())
            lastname = Trim(DsClms1.Tables(0).Rows(i).Item(2).ToString())
            designat = Trim(DsClms1.Tables(0).Rows(i).Item(3).ToString())
            job_title = Trim(DsClms1.Tables(0).Rows(i).Item(4).ToString())
            department = Trim(DsClms1.Tables(0).Rows(i).Item(5).ToString())

            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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.