Welcome to WebmasterWorld Guest from 18.206.168.65

Forum Moderators: ocean10000

Message Too Old, No Replies

Dropdownlist

     
6:08 am on Jul 31, 2009 (gmt 0)

New User

5+ Year Member

joined:July 30, 2009
posts: 6
votes: 0


Using Language: VBScript with ASP.NET
using class and User Control

I have 2 dropdownlist, example dropdownlist 1 & dropdownlist2

Dropdownlist 1 and Dropdownlist 2 have the same data

Both Dropdownlist must retrieve data from database

example
Dropdownlist 1 have 1,2,3
Dropdownlist 2 have 1,2,3

when my dropdownlist selected value="1", my dropdownlist 2 will only left 2,3 in the dropdownlist

So how can i hide the selected value ="1" from dropdownlist 2, after selected vlaue="1" from dropdownlist 1

My dropdownlist 1 selected value="1" must also trigger the Gridview, when selected the value

coding for the aspx file
<asp:Label ID="lblTitle" runat="server" Text="Reassign "
Font-Names="Calibri"></asp:Label>
asp:DropDownList ID="dropdownlist1" runat="server" AutoPostBack="True">
</asp:DropDownList>
&nbsp;
<asp:Label ID="lblTitle2" runat="server" Text="tickets to "></asp:Label>
<asp:DropDownList ID="dropdownlist2" runat="server">
</asp:DropDownList>
<br />
<br />
<asp:Label ID="lblTitle3" runat="server" Font-Names="Calibri"
Text="Reassign All"></asp:Label>
&nbsp;<asp:CheckBox ID="CBoxAll" runat="server" AutoPostBack="True" />
<br />
<asp:Label ID="lblMsg" runat="server" ForeColor="Red"></asp:Label>

coding for the aspx.vb file

For dropdownlist1_PreRender

If Page.IsPostBack = False Then
dropdownlist1.Items.Clear()
Dim clsSupportStaff_Obj As New clsSupportStaff
dt = clsSupportStaff_Obj.DBSelectByID

If dt.Rows.Count > 0 Then
Dim i As Integer

Dim ddlDefault As New ListItem
ddlDefault.Text = "-Select-"
dropdownlist1.Items.Add(ddlDefault)
For i = 0 To dt.Rows.Count - 1
Dim li As New ListItem
li.Text = dt.Rows(i)("Name0").ToString
li.Value = dt.Rows(i)("ID").ToString
dropdownlist1.Items.Add(li)
Next
End If
End If
BindGridView("Rebind")

For dropdownlist2_PreRender
If Page.IsPostBack = False Then
dropdownlist2.Items.Clear()
Dim clsSupportStaff_Obj As New clsSupportStaff
dt = clsSupportStaff_Obj.DBSelectByStatus()

If dt.Rows.Count > 0 Then
Dim i As Integer

Dim ddlDefault As New ListItem
ddlDefault.Text = "-Select-"
dropdownlist2.Items.Add(ddlDefault)
For i = 0 To dt.Rows.Count - 1
Dim li As New ListItem
li.Text = dt.Rows(i)("Name0").ToString
li.Value = dt.Rows(i)("ID").ToString
dropdownlist2.Items.Add(li)
Next
End If
End If

Thanks in advance

4:06 pm on July 31, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


You could set the DropDownList1 AutoPostBack property to true, and then re-populate DropDownList2 on the SelectedIndexChanged of DropDownList1.

Then change your code slightly:


If Not DropDownList1.SelectedValue = dt.Rows(i)("ID").ToString Then
Dim li As New ListItem
li.Text = dt.Rows(i)("Name0").ToString
li.Value = dt.Rows(i)("ID").ToString
DropDownList2.Items.Add(li)
End If

This isn't the most efficient of solutions, although you could cache the 'dt' object to save database traffic.

Another option would be to use Javascript.

7:59 am on Aug 4, 2009 (gmt 0)

New User

5+ Year Member

joined:July 30, 2009
posts: 6
votes: 0


I have 2 dropdownlist and 1 gridview

dropdownlist 1 will trigger dropdownlist 2 and gridview

When i selected dropdownlist 1 value, i don't want to show on dropdownlist 2

So how to write the SQL Statement in Class and link with my aspx.vb file?

coding for the aspx file

<asp:Label ID="lblTitle0" runat="server" Text="Reassign Ticket"></asp:Label></div>
<asp:Label ID="lblTitle" runat="server" Text="Reassign "
Font-Names="Calibri"></asp:Label>
&nbsp;<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
&nbsp;
<asp:Label ID="lblTitle2" runat="server" Text="tickets to "></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<br />
<asp:Label ID="lblTitle3" runat="server" Font-Names="Calibri"
Text="Reassign All"></asp:Label>
&nbsp;<asp:CheckBox ID="CBoxAll" runat="server" AutoPostBack="True" />
<br />
<asp:Label ID="lblMsg" runat="server" ForeColor="Red"></asp:Label>
<br />
<asp:Panel ID="Custom_MsgBox_YesNo" runat="server" Visible="True" style="position:fixed;top:0px;left:0px;" Width="100%">
<uc1:AC_ConfirmDelete ID="AC_ConfirmDelete1" runat="server" />
</asp:Panel>

<br />
<asp:UpdatePanel ID="UpdatePanelReassignTicket" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CssClass="GridViewStyle"
Width="409px">
<PagerSettings Mode="NextPreviousFirstLast" />
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<Columns>
<asp:TemplateField HeaderText="Ticket No" SortExpression="ID">
<ItemTemplate>
<asp:Label ID="lblTicketNo" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
<asp:HiddenField ID="HiddenID" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject">
<ItemTemplate>
<asp:HyperLink ID="LinkSubj" runat="server" CssClass="HyperLinkMenu_Level2"
Font-Underline="True" ForeColor="Blue" Text='<%# Eval("Desc0") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDesc" runat="server" Text='<%# Eval("Subject") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Support Staff" SortExpression="SupportStaffID">
<ItemTemplate>
<asp:Label ID="lblStaff" runat="server"
Text='<%#GetSupportStaff(Container.DataItem)%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reassign">
<ItemTemplate>
<asp:CheckBox ID="CBoxUpdate" runat="server" AutoPostBack="True" OnCheckedChanged="HandleChange" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView"
EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>

coding for aspx.vb file

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

lblMsg.Text = ""
If Page.IsPostBack = True Then
If Not (ViewState("dt") Is Nothing) Then
dt = DirectCast(ViewState("dt"), DataTable)
End If
ElseIf Page.IsPostBack = False Then
' For the First Time Then Bind the record
ViewState("SortDir") = "DESC"
ViewState("SortBLOCKED BLOCKED EXPRESSION") = "ID"
BindGridView("FirstTime")
End If
If CBoxAll.Checked = True Then
If Dropdownlist2.SelectedValue = "-Select-" Then
CBoxAll.Checked = False
lblMsg.Text = "Please select a staff you would like to reassign to."
Exit Sub
ElseIf Dropdownlist1.SelectedValue = "-Select-" Then
CBoxAll.Checked = False
lblMsg.Text = "Please select indicate which staff would you like to reassign the ticket."
ElseIf Dropdownlist2.SelectedValue <> "-Select-" Then
Dim Panel_UserControl As Panel
Dim UserControl_temp As UserControl

' Getting Data from Dataset
Dim dt_temp As New DataTable
dt_temp = DirectCast(ViewState("dt"), DataTable)

UserControl_temp = DirectCast(Custom_MsgBox_YesNo.FindControl("AC_ConfirmDelete1"), UserControl)
Panel_UserControl = DirectCast(UserControl_temp.FindControl("Panel_ConfirmDelete"), Panel)
Panel_UserControl.Visible = True

Dim lbl_msg As Label ' For Display Msg
Dim lbl_Title As Label ' For Display Msg

Dim hidden_id As HiddenField
Dim hidden_className As HiddenField

Dim dt_staff As DataTable
Dim clsSupportStaff_Obj As New clsSupportStaff
dt_staff = clsSupportStaff_Obj.DBSelect(DropDownList1.SelectedValue)
Dim staff As String
Dim staff2 As String
staff = dt_staff.Rows(0)("Name0").ToString
Dim dt_staff2 As DataTable
dt_staff2 = clsSupportStaff_Obj.DBSelect(DropDownList2.SelectedValue)
staff2 = dt_staff2.Rows(0)("Name0").ToString

lbl_msg = DirectCast(Panel_UserControl.FindControl("lbl_msg"), Label)
lbl_msg.Text = "Are you sure you want to reassign all " + staff + " tickets to " + staff2 + " ?"

lbl_Title = DirectCast(Panel_UserControl.FindControl("lbl_title"), Label)
lbl_Title.Text = " "

hidden_id = DirectCast(Panel_UserControl.FindControl("deleteID"), HiddenField)
hidden_id.Value = DropDownList2.SelectedValue

hidden_className = DirectCast(Panel_UserControl.FindControl("ClassName"), HiddenField)
hidden_className.Value = "REASSIGN"

BindGridView("ReSelect")
CBoxAll.Checked = False
End If
End If
End Sub

Sub BindGridView(Optional ByVal IsDataBaseSelect As String = "")
If IsDataBaseSelect <> "" Then
Dim ClsTickets_Obj As New clsTickets
If Dropdownlist1.SelectedValue = "" Or Dropdownlist1.SelectedValue = "-Select-" Then
dt = ClsTickets_Obj.DBSelectByStaff()
Else
dt = ClsTickets_Obj.DBSelectByStaff(Dropdownlist1.SelectedValue)
End If

ViewState("dt") = dt
Else
dt = DirectCast(ViewState("dt"), DataTable)
End If

If Not (ViewState("SortExpression") Is Nothing) Then
dt.DefaultView.Sort = ViewState("SortExpression")
End If

GridView1.DataSource = dt
GridView1.DataBind()

End Sub

Protected Sub DropDownList1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.PreRender

If Page.IsPostBack = False Then
DropDownList1.Items.Clear()
Dim clsSupportStaff_Obj As New clsSupportStaff
dt = clsSupportStaff_Obj.DBSelectByID

If dt.Rows.Count > 0 Then
Dim i As Integer

Dim ddlDefault As New ListItem
ddlDefault.Text = "-Select-"
DropDownList1.Items.Add(ddlDefault)
For i = 0 To dt.Rows.Count - 1
Dim li As New ListItem
li.Text = dt.Rows(i)("Name0").ToString
li.Value = dt.Rows(i)("ID").ToString
DropDownList1.Items.Add(li)
Next
End If
End If
BindGridView("Rebind")
End Sub

Protected Sub Dropdownlist2_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Dropdownlist2.PreRender

If Page.IsPostBack = False Then
Dropdownlist2.Items.Clear()
Dim clsSupportStaff_Obj As New clsSupportStaff
dt = clsSupportStaff_Obj.DBSelectByStatus()

If dt.Rows.Count > 0 Then
Dim i As Integer

Dim ddlDefault As New ListItem
ddlDefault.Text = "-Select-"
Dropdownlist2.Items.Add(ddlDefault)
For i = 0 To dt.Rows.Count - 1
Dim li As New ListItem
li.Text = dt.Rows(i)("Name0").ToString
li.Value = dt.Rows(i)("ID").ToString
Dropdownlist2.Items.Add(li)
Next
End If
End If
End Sub

DropDownList1_PreRender link with class

Public Function DBSelectByID(Optional ByVal ID As Integer = 0) As DataTable
Dim local_dbConn As New SqlConnection
Dim local_sqlCommand As New SqlCommand

local_dbConn = db_conn.DBConn
local_sqlCommand.Connection = local_dbConn

If ID = 0 Then
local_sqlCommand.CommandText = "SELECT * FROM ITHelpdesk.dbo.SupportStaff order by Name0"
Else
local_sqlCommand.CommandText = "SELECT * FROM ITHelpdesk.dbo.SupportStaff WHERE ID = @XID order by Name0"
local_sqlCommand.Parameters.Add(New SqlParameter("@xID", ID))

End If

local_sqlCommand.CommandType = CommandType.Text

Dim sql_adapter As New SqlDataAdapter(local_sqlCommand)
Dim dt As New DataTable
sql_adapter.Fill(dt)
If dt.Rows.Count > 0 Then
dt.PrimaryKey = New DataColumn() {dt.Columns("ID")}
End If

local_dbConn.Close()
Return dt

End Function

DropDownList2_PreRender link with class
Public Function DBSelectByStatus(Optional ByVal ID As Integer = 0) As DataTable
Dim local_dbConn As New SqlConnection
Dim local_sqlCommand As New SqlCommand

local_dbConn = db_conn.DBConn
local_sqlCommand.Connection = local_dbConn

If ID = 0 Then
local_sqlCommand.CommandText = "SELECT * FROM ITHelpdesk.dbo.SupportStaff where status0 = 'true' order by Name0"
Else
local_sqlCommand.CommandText = "SELECT * FROM ITHelpdesk.dbo.SupportStaff WHERE ID = @XID and status0 = 'true' order by Name0"
local_sqlCommand.Parameters.Add(New SqlParameter("@xID", ID))
local_sqlCommand.Parameters.Add(New SqlParameter("true", Status0))

End If

local_sqlCommand.CommandType = CommandType.Text

Dim sql_adapter As New SqlDataAdapter(local_sqlCommand)
Dim dt As New DataTable
sql_adapter.Fill(dt)
If dt.Rows.Count > 0 Then
dt.PrimaryKey = New DataColumn() {dt.Columns("ID")}
End If

local_dbConn.Close()
Return dt

End Function

So how should i change my SQL statement for my "DropDownList2_PreRender link with class" -Public Function DBSelectByStatus" so to make my dropdownlist 2 will hide the value which selected from dropdownlist 1

And also for DropDownList2_PreRender in aspx.vb file, how should i change?

Thank in Advance

3:18 am on Aug 6, 2009 (gmt 0)

New User

5+ Year Member

joined:July 30, 2009
posts: 6
votes: 0


Got anyone know how to do, please help.

Thank in advance