DropDownList SelectedIndexChanged within Gridview not firing!
Asked Answered
S

2

5

I have been looking for a solution to this for a while though and seen many posts that show me how to do it but yet I cannot get my SelectedIndexChanged event to fire when the DropDownList is changed.

The DropDownList AutoPostBack is set to True, i've followed code in the below post also: Link to post

Here is my code:

.ASPX

    <asp:GridView ID="gvCases" DataKeyNames="UserId" runat="server" AutoGenerateColumns="False" 
    BorderWidth="0px" CssClass="gridList" GridLines="None">
    <AlternatingRowStyle BackColor="#F7F7F7" />
    <Columns>

        <asp:BoundField DataField="id" HeaderText="Case Ref" />

        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="clientName" runat="server" Text="Label"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="company" HeaderText="Company" />

        <asp:TemplateField HeaderText="Order Date">
            <ItemTemplate>
                <asp:Label ID="dateTime" runat="server" Text="Label"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Case Owner">
            <ItemTemplate>
                <asp:DropDownList ID="iconUsers" runat="server" OnSelectedIndexChanged="iconUsers_SelectedIndexChanged">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:Button ID="btnDetails" runat="server" CausesValidation="False" Text="Details" />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:Button ID="btnSchedule" runat="server" CausesValidation="False" Text="Schedule" />
            </ItemTemplate>
        </asp:TemplateField>



    </Columns>
</asp:GridView>

.VB

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


    If (Request.IsAuthenticated = False) Then
        Response.Redirect("~/admin/default.aspx")
    End If


    Dim keypadSQL As SqlConnection = New SqlConnection()
    keypadSQL.ConnectionString = ConfigurationManager.ConnectionStrings("connKeypad").ConnectionString()


    Dim cmdActive As SqlCommand = New SqlCommand()
    cmdActive.Connection = keypadSQL
    cmdActive.CommandText = "spCasesActive"
    cmdActive.CommandType = CommandType.StoredProcedure


    Dim daCases As SqlDataAdapter = New SqlDataAdapter
    daCases.SelectCommand = cmdActive

    Dim dsCases As DataSet = New DataSet()
    daCases.Fill(dsCases, "CaseList")

    Dim CaseTotal As Integer
    CaseTotal = dsCases.Tables(0).Rows.Count

    If CaseTotal = 1 Then
        iCaseTotal.InnerHtml = CaseTotal & " Case"
    Else
        iCaseTotal.InnerHtml = CaseTotal & " Cases"
    End If

    gvCases.DataSource = dsCases
    gvCases.DataBind()
    cmdActive.Dispose()


    If Page.IsPostBack Then

    End If

End Sub

Protected Sub gvCases_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCases.RowDataBound

    If e.Row.RowType = DataControlRowType.Header Then

        gvCases.Columns(5).ItemStyle.Width() = "60"
        gvCases.Columns(6).ItemStyle.Width() = "70"

    End If

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)

        Dim strClientName As String
        Dim clientName As Label
        strClientName = rowView("firstname") & " " & rowView("lastname")
        clientName = CType(e.Row.FindControl("clientName"), Label)
        clientName.Text = strClientName

        Dim strDateTime As String
        Dim dateTime As Label
        strDateTime = rowView("CaseSent")
        dateTime = CType(e.Row.FindControl("dateTime"), Label)
        dateTime.Text = FormatDateTime(strDateTime, DateFormat.ShortDate) & "<br />" & FormatDateTime(strDateTime, DateFormat.ShortTime)

        gvCases.Columns(3).ItemStyle.Font.Size = 8
        gvCases.Columns(5).ControlStyle.CssClass = "btnEdit"
        gvCases.Columns(6).ControlStyle.CssClass = "btnSchedule"

        Dim intUserId As String
        intUserId = Convert.ToString(gvCases.DataKeys(e.Row.RowIndex).Value)



        Dim cmd As New SqlCommand("SELECT id, Firstname, Lastname, Firstname + ' ' + Lastname As FullName FROM [users_icon] ORDER BY Firstname, Lastname", New SqlConnection(ConfigurationManager.ConnectionStrings("connKeypad").ConnectionString()))
        cmd.Connection.Open()

        Dim ddlValues As SqlDataReader
        ddlValues = cmd.ExecuteReader()

        Dim iconUsers As DropDownList
        iconUsers = CType(e.Row.FindControl("iconUsers"), DropDownList)
        iconUsers.Style.Add("font-size", "11px")
        iconUsers.DataSource = ddlValues
        iconUsers.DataValueField = "id"
        iconUsers.DataTextField = "FullName"
        iconUsers.DataBind()

        Dim ListItem1 = New ListItem("Select Case Owner", "0")
        iconUsers.Items.Insert("0", ListItem1)
        iconUsers.AutoPostBack = True
        If IsDBNull(rowView("CaseOwner")) Then
            iconUsers.SelectedValue = 0
        Else
            iconUsers.SelectedValue = rowView("CaseOwner")
        End If

        AddHandler iconUsers.SelectedIndexChanged, AddressOf iconUsers_SelectedIndexChanged

        cmd.Connection.Close()
        cmd.Connection.Dispose()


        Dim btnDetails As Button = CType(e.Row.FindControl("btnDetails"), Button)
        btnDetails.PostBackUrl = "~/admin/detail.aspx?uid=" & intUserId

        Dim LabelAddress As Button = CType(e.Row.FindControl("btnSchedule"), Button)
        LabelAddress.PostBackUrl = "~/admin/schedule.aspx?uid=" & intUserId

    End If

End Sub

Protected Sub iconUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Response.Write("Function Called")

End Sub

Thanks for any help. J.

Submarginal answered 6/5, 2011 at 13:5 Comment(3)
Yes it is, within gvCases_RowDataBound - iconUsers.AutoPostBack = TrueSubmarginal
Is ViewState enabled or disabled?Effervesce
Set to Inherit on gridview and dropdownmenu, should just be set to default for the page so i guess thats enabled!?Submarginal
B
6

There are some similar questions (see Event handler not firing using AddHandler and Assign an event to a custom control inside a Repeater control), but your particular case looks like you're adding the handler twice; once in the markup, and once on databound.

I'd remove the one in the RowDataBound event (as it's not doing anything, because the handler will be lost when you do post back, and the handler is added after the event would actually fire). Also, make sure you AutoPostBack as @Bala mentions.

Bahia answered 6/5, 2011 at 13:13 Comment(6)
I've slightly changed my code, i've taken the event handler and autopostback out of code behind and put in my .ASPX - <asp:DropDownList ID="iconUsers" runat="server" OnSelectedIndexChanged="iconUsers_SelectedIndexChanged" AutoPostBack="true">, but it's still not firing :(, i'm well and truly stuck.Submarginal
So the page is posting back, you're debugging, and it never hits the breakpoint you've set at iconUsers_SelectedIndexChanged?Bahia
Yeah thats correct ian, never gets to the breakpoint when I change one of the dropdownlists.Submarginal
Ah - it looks like you're re-databinding every dropdown on every row (even on postback), and you are calling gvCases.DataBind() in Page_Load before the event gets handled. So when you re-databind the DropDownList, you'll clear the SelectedIndexChanged event that fired as well. Is there a reason you're explicitly DataBinding in Page_Load, rather setting the GridView's datasource to some SQLDataSource and letting it happen on its own?Bahia
oh right, i think i get what your saying. Theres no reason why I don't use the grid views SQLDataSource, just the way i like doing it and keep all data logic away from front end. But I added 'If Not Page.IsPostBack Then' around the data binding in Page_Load and I seem to be getting somewhere, the SelectedIndexChanged event is now firing :). I should hopefully now be able to get the rest working with a little play.Submarginal
Didn't take to long to get finished after you worked out the issue with the postback, can't believe it was such a small thing. I now have it updating the database with the selected item from the dropdownlist. Thanks very much for your effort and help.Submarginal
C
4

You say AutoPostBack is set to true but I don't see it in the markup and by default it's set to false. So try

<asp:DropDownList ID="iconUsers" runat="server" OnSelectedIndexChanged="iconUsers_SelectedIndexChanged" AutoPostBack="true">
            </asp:DropDownList>
Cascade answered 6/5, 2011 at 13:10 Comment(1)
Hi Bala, it is in code behind in gvCases_RowDataBound this line: iconUsers.AutoPostBack = True. I can see that the page is posting back also when I change the dropdownlist, but the event won't fire.Submarginal

© 2022 - 2024 — McMap. All rights reserved.