2014-09-20 56 views
0

条款我有这样的下拉列表: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 

回答

0

我已经解决了这个问题,并希望分享它,以防将来帮助其他人。

新增onselectedindexchanged的下拉列表

<asp:DropDownList ID="ddlEvents" runat="server" AutoPostBack="True" Width="140px" onselectedindexchanged="ddlEvents_SelectedIndexChanged" > 
    <asp:ListItem Value="0">Choose Location</asp:ListItem> 
    </asp:DropDownList> 

创造了SelectedIndexChange子,并呼吁BindGrid分从那里:

Protected Sub ddlEvents_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) 
     BindGrid() 
    End Sub 

而且它解决了我的问题。

0

直接回答你的问题,采取了在BindGrid的addHandler操作代码()。添加参数BindGrid()选定的指数变化事件所需,及导线上事件标记:

<asp:DropDownList ID="ddlEvents" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="BindGrid"> 
      <asp:ListItem Value="0">Choose Location</asp:ListItem> 
     </asp:DropDownList> 


    Public Sub BindGrid(ByVal Sender As Object, ByVal e As EventArgs) 
     Dim oconn As New SqlConnection(sqlconn)   
     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 

不过,我想提出一个更简单的解决方案。您根本不需要处理SelectedIndexChanged,以便将其用作查询参数。马克它是这样的:

<asp:DropDownList ID="ddlEvents" runat="server" AppendDataBoundItems="true" AutoPostBack="true" > 
     <asp:ListItem Value="0">Choose Location</asp:ListItem> 
</asp:DropDownList> 

标记您的网格像这样用的DataSourceID指一个SqlDataSource(如下图所示):

​​

最后创建网格一个SqlDataSource并用它配置参数(防止SQL注入攻击),可以自动从您的DropDownList拉:

<asp:SqlDataSource runat="server" 
SelectCommand="SELECT e.eventsId, e.Location, 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 = @eventId)"> 
<SelectParameters> 
    <asp:ControlParameter ControlID="ddlEvents" PropertyName="SelectedValue" Name="@eventId" /> 
</SelectParameters>    

现在,当用户从DropDownList中选择时,网格会自动重新查询并绑定数据。

+0

感谢您提出的解决方案。 我喜欢第一个解决方案,但它不起作用。 首先,我从下拉列表中选择一个选项,但不显示与其关联的记录。 其次,当我选择一个选项时,它会立即返回列表中的第一个选项。 我会喜欢这个选项,因为使用第二个解决方案会创建重复绑定数据源错误,因为我已经从代码隐藏绑定。 – Tairoc 2014-09-20 21:57:39

+0

您必须有其他代码干扰。你可以发布你的整个代码吗? – Crowcoder 2014-09-20 22:09:58

+0

当然。我已将它附加在上面,但很长。 非常感谢 – Tairoc 2014-09-20 22:20:25