Solved

How to run an ascx user control from an html control

Posted on 2006-11-20
11
246 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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