Search code examples
c#asp.netgridviewdropdownlistfor

asp.net DropDownList inside GridView


I'm using GridView and inside that I have DropDownList

 <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText="Product">
                <ItemTemplate>
                    <asp:DropDownList ID="drop_prod" runat="server" OnSelectedIndexChanged="drop_prod_SelectedIndexChanged"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
           <asp:TemplateField HeaderText="Email">
                <ItemTemplate>
                    <asp:DropDownList ID="drop_mail" runat="server" OnSelectedIndexChanged="drop_mail_SelectedIndexChanged"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Dropdownlist is already populated by values(dynamically) and if user will select some other value from dropdownlist I would like to get that value without postback action.

I hope you will help me.

Thanks


Solution

  • "I would like to get that value without postback action."

    If you dont want postback at all , you can catch dropdown change event in jquery.

    <asp:DropDownList  class="dropdown" ID="drop_prod" runat="server" OnSelectedIndexChanged="drop_prod_SelectedIndexChanged"></asp:DropDownList>
    

    Note class 'dropdown' above.

    $(document).ready(function(){
    
       $(".dropdown").change(function(){
    
        var selectedValue= $(this).val();
        $(".ddl-value").val(selectedValue);
    });
    
    });
    

    EDIT:

    You can put one hidden variable in page like

    <input type="hidden" id="selectedvalue" runat="server" class="ddl-value"/> 
    

    on server side access it like :

    selectedvalue.Value