Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have asked this question before but i have been asked to post again with better information. the link for my previous question is below.

How to construct DropDownList in VB.NET[^]

I will try to explain as best as possible and to give all the relevant information.

I have a search page in my application and after doing a search there are buttons within the results table that you can use to see further information. One of the buttons makes a call to another page which comes up as a popup on the search page displaying data.

On this stealth page i have a table that is rendered which works fine but i need a drop down list also and the data to fill the drop down list comes from a SQL function i have but i cannot seem to populate the drop down list.

I will post all my code so i apologize that it will be quite a lot but i really need help with this as i have been trying to do this for days now with no success.

This is the code on my search page HTML side...

XML
<%@ Page Title="" Language="VB" MasterPageFile="~/UsermanMast.master" AutoEventWireup="false" CodeFile="UserSearch.aspx.vb" Inherits="UserInfo"%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <link href="StyleSheet.css" rel="stylesheet" />
    <script type='text/javascript' ></script>
    <script src="js/external/jquery/jquery.js"></script>
    <script src="js/jquery-ui.min.js"></script>



    <script>
        $(function () {
            $("#dEditdob").datepicker({
                dateFormat: 'dd/mm/yy',
                changeMonth: true,
                changeYear: true,
                yearRange: '-100y:c+nn',
                maxDate: '-1d'
            });
        });
    </script>



    <div id="dUserSystems" runat="server" >
            <u>Systems user has access to</u><br />
                <div id="dUserData"></div>
            <br />

        <div id="ddlSysData" runat="server" >
             <u>Add new system to users account</u><br />
             <asp:DropDownList ID="sysDDL" runat="server"/>
        </div>
            <br />
            <input type="button" id="butAdd" value="Add" runat="server"/>&nbsp;
            <input type="button" onclick="butClose()" value="Close" />
    </div>



    <div id="dUserEdit" runat="server">
        <table>
            <tr>
                <td>
                Edit User Details
                </td>
            </tr>
            <tr>
                <td>
                    Login ID
                </td>
                <td>
                    <input id="dEditlogin" type="text" disabled="disabled" />
                </td>
            </tr>
            <tr>
                <td>
                    Staff ID
                </td>
                <td>
                    <input id="dEditstaff" type="text" maxlength="20" />
                </td>
            </tr>
            <tr>
                <td>
                    Surname
                </td>
                <td>
                    <input id="dEditsurname" type="text" maxlength="40"/>
                </td>
            </tr>
            <tr>
                <td>
                    First Name
                </td>
                <td>
                    <input id="dEditfirstn" type="text" maxlength="30"/>
                </td>
            </tr>
            <tr>
                <td>
                    Middlename
                </td>
                <td>
                    <input id="dEditmiddle" type="text" maxlength="40"/>
                </td>
            </tr>
            <tr>
                <td>
                    Date of Birth
                </td>
                <td>
                    <input id="dEditdob" type="text" maxlength="20" readonly="readonly" />
                </td>
            </tr>
            <tr>
                <td>
                    Prof Number
                </td>
                <td>
                    <input id="dEditprof" type="text" maxlength="20"/>
                </td>
            </tr>
            <tr>
                <td>
                    Job Title
                </td>
                <td>
                    <input id="dEditjob" type="text" maxlength="60"/>
                </td>
            </tr>
            <tr>
                <td>
                    Department
                </td>
                <td>
                    <input id="dEditdept" type="text" maxlength="60"/>
                </td>
            </tr>
            <tr>
                <td>
                    Site
                </td>
                <td>
                    <input id="dEditsite" type="text" maxlength="60"/>
                </td>
            </tr>
        </table>
        <input type="button" onclick="butUpdate()" value="Update"  />
         <input type="button" onclick="butCancel()" value="Close"  />
    </div>



    <div id="dUpdatemsg" runat="server">
        <div id="dMsg"></div>
        <input type="button" id="updateClose" onclick="msgClose()" value="Ok" />
    </div>





   <%--  <div id="dialog" title="Basic Dialog" style="display: none;">
        <p id="alertmsg">test msg</p>
    </div>--%>

    <br />

        <div id="userSearchForm">

            <div id="sHeader">
                 <asp:Label ID="uHeader" runat="server" Text="User Search"></asp:Label><br />
            </div>

            <div id="searchTest">

                <div id="userTitles">

                    <div id="slogin">
                        <label for="sLoginid">Login ID</label>
                    </div>
                    <div id="sfName">
                        <label for="sfirstName">First Name</label>
                    </div>
                    <div id="slName">
                        <label for="slastName">Surname</label>
                    </div>
                    <div id="sEsrNumb">
                        <label for="sESRNumber">ESR Number</label>
                    </div>
                    <div id="sProfNumb">
                        <label for="sprofNumber">Prof No</label>
                    </div>

              </div>

              <div id="sInput">

                <div id="sLoginInput">
                    <input id="sLoginid" name="sUserId" runat="server" autopostback="True" style="display: inline-block; width: 260px;" type="text" /><br />
                </div>
                <div id="sFnameInput">
                    <input id="sfirstName" name="sfirstName" runat="server" autopostback="True" style="display: inline-block; width: 260px;" type="text" /><br />
                </div>
                <div id="slNameInput">
                    <input id="slastName" name="slastName" runat="server" autopostback="True" style="display: inline-block; width: 260px;" type="text" /><br />
                </div>
                <div id="sEsrInput">
                    <input id="sESRnumber" name="sESRnumber" runat="server" autopostback="True" style="display: inline-block; width: 260px;" type="text" /><br />
                </div>
                <div id="sProfInput">
                    <input id="sprofNumber" name="sprofNumber" runat="server" autopostback="True" style="display: inline-block; width: 260px;" type="text" /><br />
                </div>
              </div>
           </div>
      </div>
                <br />


               <div id="sButtons">
                    <asp:Button ID="buttonSearch" class="showSystemsPop" runat="server" Text="Search" Width="107px" />&nbsp;&nbsp;

               </div>


                    <br />
                    <br />

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    <asp:Button ID="adNewUser" runat="server" Text="Add User" Width="107px" Visible="false"  />


    <br />


    <br />

        <div id ="content">
            <asp:PlaceHolder ID="placeResults" runat="server"></asp:PlaceHolder>
            <asp:Label ID="noResultOutput" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="Larger" ForeColor="#FF3300" CssClass="noResult"></asp:Label>
                    </div>








<script>



    //Function used to grab users details when admin is looking to update details
    function editUser(loginid, staffid, surname, firstname, middlename, dob, profnumb, jobtitle, department, site) {

        $("#dEditlogin").val(loginid.replace(/\#/g, " "));
        $("#dEditstaff").val(staffid.replace(/\#/g, " "));
        $("#dEditsurname").val(surname.replace(/\#/g, " "));
        $("#dEditfirstn").val(firstname.replace(/\#/g, " "));
        $("#dEditmiddle").val(middlename.replace(/\#/g, " "));
        $("#dEditdob").val(dob.replace(/\#/g, " "));
        $("#dEditprof").val(profnumb.replace(/\#/g, " "));
        $("#dEditjob").val(jobtitle.replace(/\#/g, " "));
        $("#dEditdept").val(department.replace(/\#/g, " "));
        $("#dEditsite").val(site.replace(/\#/g, " "));

            $("#ContentPlaceHolder1_dUserEdit").css("left", "200px");
            $("#ContentPlaceHolder1_dUserEdit").css("display", "block");

     }

    function butCancel() {
        $("#ContentPlaceHolder1_dUserEdit").css("display", "none");
    }


    //Function used to update user details
    function butUpdate() {

        var vUserid = escape($("#dEditlogin").val());
        var vStaffid = escape($("#dEditstaff").val());
        var vSurname = escape($("#dEditsurname").val());
        var vFirstname = escape($("#dEditfirstn").val());
        var vMiddlename = escape($("#dEditmiddle").val());
        var vDateofbirth = escape($("#dEditdob").val());
        var vProfnumb = escape($("#dEditprof").val());
        var vJobtitle = escape($("#dEditjob").val());
        var vDepartment = escape($("#dEditdept").val());
        var vSite = escape($("#dEditsite").val());

        var link = 'EditUser.aspx?userid=' + vUserid + '&staffid=' + vStaffid + '&surname=' + vSurname + '&firstname=' + vFirstname + '&middlename=' + vMiddlename + '&dob=' + vDateofbirth + '&profnumb=' + vProfnumb + '&jobtitle=' + vJobtitle + '&department=' + vDepartment + '&site=' + vSite;


        $.get(link, function (data) {
            $("#dMsg").empty().append(data);
            $("#ContentPlaceHolder1_dUpdatemsg").css("left", "200px");
            $("#ContentPlaceHolder1_dUpdatemsg").css("display", "block");
            });


    }


    function msgClose() {
        document.location.href = "UserSearch.aspx";

    }


    //Function used to grab list of systems user has access to
    function viewSystems(myUserid) {


        var link = 'fetchData.aspx?userid=' + myUserid;

        $.get(link, function (data) {

            $("#dUserData").empty().append(data);
            $("#ContentPlaceHolder1_dUserSystems").css("left", "200px");
            $("#ContentPlaceHolder1_dUserSystems").css("display", "block");
        });


      }

    function butClose() {
        $("#ContentPlaceHolder1_dUserSystems").css("display", "none");
    }



    function delUser(delUserid) {

        if (confirm("Are you sure you want to delete? This action cannot be undone.")){

            var link = 'DeleteUser.aspx?userid=' + delUserid

            $.get(link, function (data) {

            });
        }
        else
        {
            return false;
        }



    }


</script>


</asp:Content>


On this page the two parts that refer to the stealth page are a
and a JavaScript function...

XML
<div id="dUserSystems" runat="server" >
           <u>Systems user has access to</u><br />
               <div id="dUserData"></div>
           <br />

       <div id="ddlSysData" runat="server" >
            <u>Add new system to users account</u><br />
            <asp:DropDownList ID="sysDDL" runat="server"/>
       </div>
           <br />
           <input type="button" id="butAdd" value="Add" runat="server"/>&nbsp;
           <input type="button" onclick="butClose()" value="Close" />
   </div>



function viewSystems(myUserid) {


var link = 'fetchData.aspx?userid=' + myUserid;

$.get(link, function (data) {

$("#dUserData").empty().append(data);
$("#ContentPlaceHolder1_dUserSystems").css("left", "200px");
$("#ContentPlaceHolder1_dUserSystems").css("display", "block");
});


}

function butClose() {
$("#ContentPlaceHolder1_dUserSystems").css("display", "none");
}

The button within the search result is created as below with onclick event linked to the function above...

bt3 = New HtmlGenericControl("div")
bt3.InnerHtml = "<input type=button onclick=viewSystems('" & dt.Item("login_id").ToString & "') value='View systems'>"


This code is from the stealth page where my table data is created...

VB
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    'Add session check here

    If Request.QueryString("userid") <> "" Then

        Dim dsResults As New DataSet
        Dim dt As DataTableReader

        dsResults = GetUserName.systemData(Request.QueryString("userid").ToString)
        dt = dsResults.CreateDataReader

        Dim dv As New HtmlGenericControl("div")
        dv.ID = "sysData"

        Dim tb As New Table
        Dim tr As New TableRow
        Dim tc As New TableCell
        Dim lb As New Label
        Dim iCount As Integer = 1

        If dt.HasRows = True Then

            tr = New TableRow
            tr.CssClass = "tabHeader"
            tc = New TableCell
            tc.CssClass = "tabCell"
            lb = New Label

            lb.Text = "System Name"
            tc.Controls.Add(lb)
            tr.Controls.Add(tc)
            tb.Controls.Add(tr)

            tr.CssClass = "tabHeader"
            tc = New TableCell
            tc.CssClass = "tabCell"
            lb = New Label

            lb.Text = "Date Added"
            tc.Controls.Add(lb)
            tr.Controls.Add(tc)
            tb.Controls.Add(tr)


            tr.CssClass = "tabHeader"
            tc = New TableCell
            tc.CssClass = "tabCell"
            lb = New Label

            lb.Text = "Added By"
            tc.Controls.Add(lb)
            tr.Controls.Add(tc)
            tb.Controls.Add(tr)

            Do While dt.Read

                tr = New TableRow

                If iCount = 1 Then
                    tr.CssClass = "rowStyle3"
                    iCount = 0
                Else
                    tr.CssClass = "rowStyle4"
                    iCount = 1
                End If

                'tr = New TableRow
                tc = New TableCell
                tc.CssClass = "rowCell2"
                lb = New Label

                lb.Text = dt.Item("description").ToString()
                tc.Controls.Add(lb)
                tr.Controls.Add(tc)
                tb.Controls.Add(tr)

                'tr = New TableRow
                tc = New TableCell
                tc.CssClass = "rowCell2"
                lb = New Label


                lb.Text = dt.Item("date_added").ToString()
                tc.Controls.Add(lb)
                tr.Controls.Add(tc)
                tb.Controls.Add(tr)

                'tr = New TableRow
                tc = New TableCell
                tc.CssClass = "rowCell2"
                lb = New Label

                lb.Text = dt.Item("added_by").ToString()
                tc.Controls.Add(lb)
                tr.Controls.Add(tc)
                tb.Controls.Add(tr)
            Loop
            dt.Close()
            dsResults.Dispose()


            dv.Controls.Add(tb)
            sysData.Controls.Add(dv)
        Else
            Response.Write("User has not been allocated access to any systems")
        End If

    End If

    If Request.QueryString("userid") = "" Then

        Response.Write("Unable to locate systems as Login ID is missing")

    End If
End Sub



What i am having trouble with is trying to get the DropDownList populated but what i have tried so far nothing happens. The code below is everything i have tried..

1. I tried creating a DDL on the stealth page server side with the code below
VB
Dim dl As New DropDownList
           Dim dt2 As New DataSet

           dt2 = GetUserName.ddlData(Request.QueryString("userid").ToString)

           dl.DataSource = dt2
           dl.DataValueField = "description"
           dl.DataTextField = "description"
           dl.DataBind()


2. I tried coding the DDL from the search page server side..
VB
If Request.QueryString("userid") <> "" Then

   '        Dim ddlResult As New DataSet
   '        Dim dt1 As DataTableReader

   '        ddlResult = GetUserName.ddlData(Request.QueryString("userid").ToString)
   '        dt1 = ddlResult.CreateDataReader

   '        sysDDL.DataSource = dt1
   '        sysDDL.DataValueField = "description"
   '        sysDDL.DataTextField = "description"
   '        sysDDL.DataBind()
   '        sysDDL.Items.Insert(0, New ListItem(String.Empty, String.Empty))
   '        sysDDL.SelectedIndex = 0

   '    End If


3. I even tried creating another page to create the information for the DDL on page load..

VB
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

     If Request.QueryString("userid") <> "" Then

         Dim ddlResult As New DataSet
         Dim dt As DataTableReader

         ddlResult = GetUserName.ddlData(Request.QueryString("userid").ToString)
         dt = ddlResult.CreateDataReader

     End If

 End Sub


but i couldnt work out the correct function to write on the search page to call.

Again i apologize that there is a lot of code but i needed to make sure all the information is here.

I was given this info and code to try but this is very confusing for me as i dont know what to put where..

Call stealth query
Data object returns a string like “option1**option2**option3”

var arrSplit = data.split("**");
$("#").css("display", "block");

$("#").empty();
var ddl = $("#");

ddl.append("<option value=''></option>");
for (i = 0; i < arrSplit.length - 1; i++) {
ddl.append("<option value='" + arrSplit[i] + "'>" + arrSplit[i] + "</option>");
}




Thank You
Posted
Updated 21-Jan-15 21:48pm
v2
Comments
Sinisa Hajnal 26-Jan-15 2:09am    
I'm back. So, questions first:
1. Do you use jQuery? If not, why not?
2. Why such non standard format for the data?
3. Do you get the options in your div? *option1**option2* etc...

You first try, with instance of DropDownList would work if you added dl into control set of page control...or used sysDDL instead of new drop down list.

This assumes that GetUserName.ddlData really returns options with column name description.

See solution.

1 solution

VB
'Dim dl As New DropDownList
' instead of the above, use existing list which is already positioned properly
           Dim dt2 As New DataSet

           dt2 = GetUserName.ddlData(Request.QueryString("userid").ToString)

           sysDDL.DataValueField = "description"
           sysDDL.DataTextField = "description"
           sysDDL.DataSource = dt2
           sysDDL.DataBind()


Note that I've put datasource after the field settings. Anyhow, the above should work as long as your "stealth" data comes before this binding. To make it happen, try loading DDL into pre-render event. Or some timer (to wait for the whole page to load).


Second solution to think about:
- use jQuery to wait for the data and parse out your options and create ddl client-side. The problem with this is that I'm not sure it will be visible server side without some extra magic via hidden fields.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900