條款我有這樣的下拉列表:WHERE與DropDownList的選定值
<asp:DropDownList ID="ddlEvents" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" Width="140px">
<asp:ListItem Value="0">Choose Location</asp:ListItem>
</asp:DropDownList>
以上下拉列表選項動態地從數據庫中填充。
然後,我有這樣的代碼隱藏:
Public Sub BindGrid()
Dim oconn As New SqlConnection(sqlconn)
AddHandler ddlLocation.SelectedIndexChanged, New EventHandler(AddressOf ddl_SelectedIndexChanged)
oconn.Open()
Dim ocmd As New SqlCommand("select e.eventsId,e.Location, dbo.fnFormatDate(t.trainingDates, 'MON/DD/YYYY') t.eventDates, t.eventTime,t.eventDescription from tblEvents e, tblEventgDates t where e.eventid = t.eventid and e.eventid = " & ddlEvents.SelectedValue, oconn)
Dim oda As New SqlDataAdapter(ocmd)
Dim builder As New SqlCommandBuilder(oda)
Dim ds As New DataSet()
oda.Fill(ds)
gv1.DataSource = ds
gv1.DataBind()
End Sub
我們的用戶希望通過選擇下拉列表eventLocation來篩選結果,並只顯示與該位置相關聯的事件有。
上面的代碼沒有做任何事情。
我懷疑我需要selectedIndexChanged
?
但是,如何將其納入BindData()
事件?
感謝很多提前
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Partial Class Events
Inherits System.Web.UI.Page
Private sqlconn As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ToString()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid()
End If
PopulateDates()
End Sub
Public Sub BindGrid()
Dim oconn As New SqlConnection(sqlconn)
' AddHandler ddlEvents.SelectedIndexChanged, New EventHandler(AddressOf ddl_SelectedIndexChanged)
oconn.Open()
Dim ocmd As New SqlCommand("select e.eventsId,e.Location, dbo.fnFormatDate(t.trainingDates, 'MON/DD/YYYY') t.eventDates, t.eventTime,t.eventDescription from tblEvents e, tblEventgDates t where e.eventid = t.eventid and e.eventid = " & ddlEvents.SelectedValue, oconn)
Dim oda As New SqlDataAdapter(ocmd)
Dim builder As New SqlCommandBuilder(oda)
Dim ds As New DataSet()
oda.Fill(ds)
gv1.DataSource = ds
gv1.DataBind()
End Sub
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim oconn As New SqlConnection(sqlconn)
oconn.Open()
Dim ocmd As New SqlCommand("SELECT* FROM Events", oconn)
Dim oda As New SqlDataAdapter(ocmd)
Dim builder As New SqlCommandBuilder(oda)
Dim ds As New DataSet()
oda.Fill(ds)
Dim ddl As DropDownList = DirectCast(e.Row.FindControl("ddlInstructors"), DropDownList)
If ddl IsNot Nothing Then
ddl.DataSource = ds
ddl.DataValueField = "EventsId"
ddl.DataTextField = "EventName"
ddl.DataBind()
End If
If e.Row.RowType = DataControlRowType.Footer Then
Dim ddldesig As DropDownList = DirectCast(e.Row.FindControl("ddladddesig"), DropDownList)
ddldesig.DataSource = ds
ddldesig.DataValueField = "EventsId"
ddldesig.DataTextField = "EventName"
ddldesig.DataBind()
End If
End Sub
Protected Sub gv1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim EID As Integer = Convert.ToInt32(gv1.DataKeys(e.RowIndex).Value)
Dim oconn As New SqlConnection(sqlconn)
oconn.Open()
Dim ocmd As New SqlCommand()
ocmd.CommandText = "DELETE FROM Events WHERE [email protected]"
ocmd.Parameters.AddWithValue("@EID", EID)
ocmd.Connection = oconn
ocmd.ExecuteNonQuery()
oconn.Close()
BindGrid()
End Sub
Protected Sub gv1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
gv1.EditIndex = e.NewEditIndex
BindGrid()
End Sub
Protected Sub gv1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim EID As Integer = Convert.ToInt32(gv1.DataKeys(e.RowIndex).Value)
'Dim ENAME As String = DirectCast(gv1.Rows(e.RowIndex).Cells(1).FindControl("txtename"), TextBox).Text
Dim DESIGID As Integer = Integer.Parse(DirectCast(gv1.Rows(e.RowIndex).Cells(1).FindControl("ddlInstructors"), DropDownList).SelectedValue)
Dim oconn As New SqlConnection(sqlconn)
oconn.Open()
Dim ocmd As New SqlCommand()
ocmd.CommandText = "UPDATE MainEvents SET [email protected] WHERE [email protected] "
ocmd.Parameters.AddWithValue("@EID", EID)
ocmd.Parameters.AddWithValue("@DESIGID", DESIGID)
ocmd.Connection = oconn
ocmd.ExecuteNonQuery()
gv1.EditIndex = -1
BindGrid()
End Sub
Protected Sub gv1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
gv1.EditIndex = -1
BindGrid()
End Sub
Public Sub PopulateDates()
Dim cmd As New SqlCommand("Select EventsId, eventName from tblEvents order by location asc", New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString))
cmd.Connection.Open()
ddlEvents.Items.Clear()
Dim ddlValues As SqlDataReader
ddlValues = cmd.ExecuteReader()
ddlEvents.DataSource = ddlValues
ddlEvents.DataValueField = "EventsId"
ddlEvents.DataTextField = "EventName"
ddlEvents.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub
Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
' Indicate whether the update operation succeeded.
If e.Exception Is Nothing Then
Dim index As Integer = gv1.EditIndex
Dim row As GridViewRow = gv1.Rows(index)
Message.Text = "Row updated successfully'!"
Else
e.ExceptionHandled = True
Message.Text = e.Exception.Message
End If
End Sub
End Class
感謝您提出的解決方案。 我喜歡第一個解決方案,但它不起作用。 首先,我從下拉列表中選擇一個選項,但不顯示與其關聯的記錄。 其次,當我選擇一個選項時,它會立即返回列表中的第一個選項。 我會喜歡這個選項,因爲使用第二個解決方案會創建重複綁定數據源錯誤,因爲我已經從代碼隱藏綁定。 – Tairoc 2014-09-20 21:57:39
您必須有其他代碼干擾。你可以發佈你的整個代碼嗎? – Crowcoder 2014-09-20 22:09:58
當然。我已將它附加在上面,但很長。 非常感謝 – Tairoc 2014-09-20 22:20:25