带有DropDownList选择值的WHERE子句

时间:2014-09-20 21:05:26

标签: asp.net vb.net

我有这个下拉列表:

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

以上下拉列表选项是从数据库中动态填充的。

然后我在codebehind上有这个:

    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

我们的用户希望通过从dropdownList中选择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 CourseID=@EID"
        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 EventsId=@DESIGID WHERE CourseID=@EID "
        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

2 个答案:

答案 0 :(得分: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>

使用引用SqlDataSource的DataSourceID标记您的网格(如下所示):

<asp:GridView ID="gridView1" runat="server" DataSourceID="GridSqlSrc">
</asp:GridView>

最后为您的网格创建一个SqlDataSource,并使用一个自动从DropDownList中提取的参数(防止SQL注入攻击)对其进行配置:

<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中选择时,网格会自动重新查询并绑定数据。

答案 1 :(得分: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 sub:

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

它解决了我的问题。

相关问题