Saturday, September 3, 2011

Cascading DropDownList With Database Example in GridView


Ref 1: With using ajax cascading dropdown list

http://csharpdotnetfreak.blogspot.com/2009/02/ajax-cascading-dropdownlist-database.html

Way 2 :If without using ajax cascading dropdown you can do like this


html code
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="DlBillingDetails_RowDataBound"
                        ShowHeaderWhenEmpty="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
                        GridLines="None" OnRowCommand="dlBillingDetails_RowCommand">
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        <Columns>
                            <asp:TemplateField HeaderText="Operating Company">
                                <ItemTemplate>
                                    <asp:UpdatePanel ID="updPnlOpComp" runat="server" UpdateMode="Conditional">
                                        <ContentTemplate>
                                            <asp:DropDownList ID="ddOperatingCompany" runat="server" AutoPostBack="true" ToolTip='<%#DataBinder.Eval(Container, "DataItemIndex")%>'
                                             OnSelectedIndexChanged="ddOperatingCompany_SelectedIndexChanged">
                                            </asp:DropDownList>
                                        </ContentTemplate>
                                    </asp:UpdatePanel>
                                </ItemTemplate>
                            </asp:TemplateField> 
                            <asp:TemplateField HeaderText="Type of Service">
                                <ItemTemplate>
                                    <asp:UpdatePanel ID="updPnlTOS" runat="server" UpdateMode="Conditional">
                                        <ContentTemplate>
                                            <asp:DropDownList ID="ddtypeOfService" runat="server">
                                            </asp:DropDownList>
                                        </ContentTemplate>
                                    </asp:UpdatePanel>
                                </ItemTemplate>
                            </asp:TemplateField>                          
                        </Columns>
                        <EditRowStyle BackColor="#999999" />
                        <EmptyDataTemplate>
                            No records found!
                        </EmptyDataTemplate>
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    </asp:GridView>



And the C# code is like below ( only for populating the 2nd dropdown)


 protected void ddOperatingCompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            DropDownList ddOperatingCompany = (DropDownList)sender;
            int rowid = Convert.ToInt32(ddOperatingCompany.ToolTip);
            DropDownList ddtypeOfService = (DropDownList)dlBillingDetails.Rows[rowid].FindControl("ddtypeOfService");
            if (ddtypeOfService != null)
            {
                FillCombo.TypeOfServices(ref ddtypeOfService, Convert.ToInt32(ddOperatingCompany.SelectedValue)); // my method to fill dropdowns
            }
            UpdatePanel updPnlTOS = (UpdatePanel)dlBillingDetails.Rows[rowid].FindControl("updPnlTOS");
            if (updPnlTOS != null)
            {
                updPnlTOS.Update(); 
            }
        }
        catch (Exception ex)
        {
            Constants.log.Exception("UserControls_EmpBillingAssignment", "ddOperatingCompany_SelectedIndexChanged", "", ex);
        }
    }

Way 3 : withoud ajax

      just use way 2 but just avoid using the update panel and page would refresh.

Feel free to add your comments \ suggestions \ queries \ better soluions.

2 comments:

  1. Good one,But can you give me the same one with out using ajax

    ReplyDelete
    Replies
    1. you can try http://www.aspsnippets.com/Articles/Creating-Cascading-DropDownLists-in-ASP.Net.aspx or many more links at http://www.codeproject.com/Questions/433022/Cascading-Country-City-drop-down-list-in-asp-net

      Delete

Devops links

  Build Versioning in Azure DevOps Pipelines