Solved

How to run an ascx user control from an html control

Posted on 2006-11-20
11
243 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:lnshop
  • 6
  • 5
11 Comments
 
LVL 9

Expert Comment

by:kraffay
ID: 17982058
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
 

Author Comment

by:lnshop
ID: 17982266
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
 
LVL 9

Expert Comment

by:kraffay
ID: 17982741
Can you post the UC markup?
0
 

Author Comment

by:lnshop
ID: 17983218
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
 
LVL 9

Expert Comment

by:kraffay
ID: 17983403
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:lnshop
ID: 17983513
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
 
LVL 9

Expert Comment

by:kraffay
ID: 17983590
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
 

Author Comment

by:lnshop
ID: 17983716
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
 
LVL 9

Accepted Solution

by:
kraffay earned 125 total points
ID: 17983816
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
 

Author Comment

by:lnshop
ID: 17983881
Thank-you very much.  I will give that a try tomorrow morning.
0
 

Author Comment

by:lnshop
ID: 17997603
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now