OnSelectedIndexChanged of the DropDownList in a GridView's PagerTemplate is not fired

Last post 12-17-2009 1:26 AM by roopeshreddy. 7 replies.

Sort Posts:

  • OnSelectedIndexChanged of the DropDownList in a GridView's PagerTemplate is not fired

    08-30-2008, 12:26 PM
    • Member
      point Member
    • joanna_cyt
    • Member since 08-30-2008, 3:50 PM
    • Posts 4

    Hi, I got information from here quite often.

    I just want to share my experience this time and hope it might help someone someday.

    I have a DropDownList in my GridView's PagerTemplate.

    I used the example code from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pagertemplate(VS.80).aspx

    I used it in many of my pages. All works fine except one.

    The OnSelectedIndexChanged event just isn't fired in that one page.

    Later I set the "EnableViewState" of that GridView to false, it works finally.

     

  • Re: OnSelectedIndexChanged of the DropDownList in a GridView's PagerTemplate is not fired

    11-18-2008, 3:39 PM
    • Member
      14 point Member
    • goodbrat_in
    • Member since 11-12-2008, 7:43 PM
    • Posts 7

     Hi I am newbie in ASP .net. I have a gridview with the first column as a template field of dropdownlist. All the other fields in the Gridview are have a value of  0 or 1.The dropdownlist has three items viz. "Make All 0", "Make All 1" and "Edit". What I would like to do is that when a user selects "Make All 0", all the other fields for that row should change to 0. Similarly for "Make All 1" all the fields in the that row should change to 1. And if the user selects "Edit" from the list the user should be able to edit the row, i.e. set the value of each of the fields one by one. Again, these values can either be 0 or 1.

    My problem is I don't know how to trigger an event just by selecting an item from the dropdownlist which is within a gridview.

    I would really appreciate it if you could help me out with this.

    I am using VB for coding. But even if you have a solution in C# I don't mind taking a look at it.

    Thanks Smile

  • Re: OnSelectedIndexChanged of the DropDownList in a GridView's PagerTemplate is not fired

    11-19-2008, 10:52 AM
    • Member
      point Member
    • joanna_cyt
    • Member since 08-30-2008, 3:50 PM
    • Posts 4

    In your .aspx file,  you have something like the following. You define DropDownList1_SelectedIndexChanged() in your .vb file.

    <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                                        <asp:ListItem Value="0">0</asp:ListItem>
                                        <asp:ListItem Value="1">1</asp:ListItem>
                                        </asp:DropDownList>

    </ItemTemplate>

    </asp:TemplateField>

  • Re: OnSelectedIndexChanged of the DropDownList in a GridView's PagerTemplate is not fired

    11-20-2008, 2:19 PM
    • Member
      14 point Member
    • goodbrat_in
    • Member since 11-12-2008, 7:43 PM
    • Posts 7

     Thanks for the reply. But, I am still not able to achieve what I intend to. SelectedIndexChanged doesn't trigger an event. Here is what i did

    asp code

     

    <asp:TemplateField>
                         <ItemTemplate>
                                        <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                                                <asp:ListItem Value="0">Make all 0</asp:ListItem>
                                                <asp:ListItem Value="1">Make all 1</asp:ListItem>
                                                <asp:ListItem Value="2">Edit</asp:ListItem>
                                                <asp:ListItem Selected="True" > </asp:ListItem>
                                      </asp:DropDownList>>

                         </ItemTemplate>

    </asp:TemplateField>

     

    And the vb code i used is;

     

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

     If CType(GridView1.Rows(GridView1.SelectedIndex).Cells(0).FindControl("DropDownList1"), DropDownList).SelectedValue = 0 Then
                For i As Integer = 3 To 33
                    GridView1.Rows(GridView1.SelectedIndex).Cells(i).Text = 0
                Next
            End If
            If CType(GridView1.Rows(GridView1.SelectedIndex).Cells(0).FindControl("DropDownList1"), DropDownList).SelectedValue = 1 Then
                For i As Integer = 3 To 33
                    GridView1.Rows(GridView1.SelectedIndex).Cells(i).Text = 1
                Next
            End If

     End Sub

     

    I don't think that the subroutine is even called once. I am not sure how to do it without using any other control like a button or a link. I want it to be such that when the user selects the list item, the subroutine is called.

    Please let me know how to achieve this. Thanks for your help.

    Smile

  • Re: OnSelectedIndexChanged of the DropDownList in a GridView's PagerTemplate is not fired

    11-21-2008, 1:50 AM
    • Member
      point Member
    • joanna_cyt
    • Member since 08-30-2008, 3:50 PM
    • Posts 4

    Sorry, I forgot to set AutoPostBack to true. 

                                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

  • Re: OnSelectedIndexChanged of the DropDownList in a GridView's PagerTemplate is not fired

    11-24-2008, 11:12 AM
    • Member
      14 point Member
    • goodbrat_in
    • Member since 11-12-2008, 7:43 PM
    • Posts 7

     I can finally make all the rows 0 or 1 using the following code;


        Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim ddl As DropDownList = DirectCast(sender, DropDownList)
            Dim row As GridViewRow = TryCast(ddl.NamingContainer, GridViewRow)
            Dim i As Int16
            If row IsNot Nothing Then
                If CType(row.FindControl("DropDownList1"), DropDownList).SelectedValue = "0" Then
                    For i = 3 To 33
                        row.Cells(i).Text = 0
                    Next
                ElseIf CType(row.FindControl("DropDownList1"), DropDownList).SelectedValue = "1" Then
                    For i = 3 To 33
                        row.Cells(i).Text = 1
                    Next

                End If
            End If
        End Sub

     

    But I still have to figure out how to edit all the columns in a row, i.e. if the user selects "Edit" from the dropdownlist, all the columns in that specific row should become editable. Also, I would like to update these changes in the database. How am I supposed to do that???

  • Re: OnSelectedIndexChanged of the DropDownList in a GridView's PagerTemplate is not fired

    11-25-2008, 11:24 AM
    • Member
      point Member
    • joanna_cyt
    • Member since 08-30-2008, 3:50 PM
    • Posts 4

    In my experience, I don't put "Edit" option in the DropDownList. I use a button in each row and specify its CommandName "Edit". First of all I create EditItemTemplate in the GridView. When I click the edit button, the GridView will be in its edit mode. If you want to update the database, you just put a button with CommandName "Update" in your EditItemTemplate. Your GridView would look like the following,

    <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"...>

    <ItemTemplate>

    ...

    </ItemTemplate> 

     <EditItemTemplate>

    ...

    </EditItemTemplate>

    </asp:GridView>

     

    You write your update procedure in GridView1_RowCommand().

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

    {

    switch(e.CommandName)

    {

    case "Update":

    ....

    }

    }

     

  • Re: OnSelectedIndexChanged of the DropDownList in a GridView's PagerTemplate is not fired

    12-17-2009, 1:26 AM
    • Member
      7 point Member
    • roopeshreddy
    • Member since 05-24-2009, 3:53 AM
    • India
    • Posts 7

    Hi,

      Thanks a lot. It worked for me.... 

    Thanks & Regards
    Roopesh Reddy C
Page 1 of 1 (8 items)