I have this dropdownlist:
<asp:DropDownList ID="ddlEvents" runat="server" AppendDataBoundItems="True" AutoPostBack="True" Width="140px"> <asp:ListItem Value="0">Choose Location</asp:ListItem> </asp:DropDownList> Above dropdownlist options are dynamically populated from the database.
Then I have this on 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 Our users would like to filter results by selecting eventLocation from the dropdownList and have only events associated with that location to be displayed.
Code above is not doing anything.
I suspect that I need selectedIndexChanged?
But how do I incorporate it into the BindData() event?
Thanks much in advance
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