如何在GridView中对多个下拉列表值进行排序 [英] How to sort multiple dropdown list values in gridview

查看:71
本文介绍了如何在GridView中对多个下拉列表值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友
我有三个下拉菜单名称,分别是主要类别",子类别"和显示值"的产品,但是我必须根据第一个下拉菜单值选择第二个下拉菜单值,即当我单击或选择基于主菜单的第一个下拉菜单时类别子类别ID必须在第二个下拉列表中显示....

我的代码是

Hi friends
i have three drop downs names as Main category,subcategory ,and products which ware displaying values , but i have to select second drop down values on the basis of first drop down values ie when i click or select first drop down on the basis of main category subcategory id has to be shown in second drop down ....

my code is

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default16.aspx.cs" Inherits="Default16" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:gridview ID="Gridview1" runat="server" ShowFooter="true"

            AutoGenerateColumns="false"

            onselectedindexchanged="Gridview1_SelectedIndexChanged"

            onrowdatabound="Gridview1_RowDataBound">
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />

           <asp:TemplateField HeaderText="MainCategory">
                <ItemTemplate>
                    <asp:DropDownList  ID="DropDownList1" runat="server" >
                    </asp:DropDownList >
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="SubCategory">
                <ItemTemplate>
                    <asp:DropDownList  ID="DropDownList2" runat="server" >
                    </asp:DropDownList >
                </ItemTemplate>
            </asp:TemplateField>

           <asp:TemplateField HeaderText="products">
                <ItemTemplate>
                    <asp:DropDownList  ID="DropDownList3" runat="server" >
                    </asp:DropDownList >
                </ItemTemplate>
            </asp:TemplateField>



            <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField >
        <ItemTemplate >

                 <asp:Button ID="ButtonAdd" runat="server"

                             Text="Add New Row"

                             onclick="ButtonAdd_Click" />


        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField >
        <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
                 <asp:Button ID="ButtonInsert" runat="server"

                             Text="Insert"

                             onclick="ButtonInsert_Click" />
            </FooterTemplate>
        </asp:TemplateField>

            </Columns>
        </asp:gridview>
    </div>
    </form>
</body>
</html>

推荐答案

好,这有点麻烦.因此,首先,您需要绑定主类别下拉列表(此处显示示例):

Ok this was a bit of a work. So first, you need to bind the main category dropdownlist (sample shown here):

protected void RowBound(object sender, GridViewRowEventArgs e)
        {

            if (((DropDownList)e.Row.FindControl("DropDownList1")) != null)
            {
                ((DropDownList)e.Row.FindControl("DropDownList1")).Items.Add(new ListItem("A", "1"));
                ((DropDownList)e.Row.FindControl("DropDownList1")).Items.Add(new ListItem("B", "2"));
                ((DropDownList)e.Row.FindControl("DropDownList1")).Items.Add(new ListItem("C", "3"));
            }
        }



现在,在此DropDownList1的选定索引更改事件上,根据在DropDownList1中选择的内容将数据绑定到DropDownList2:



Now, On the selected index changed event of this DropDownList1, bind the data to the DropDownList2 based on what you select in DropDownList1:

protected void Change(object sender, EventArgs e)
        {
            DropDownList ddlzz = (DropDownList)this.FindControlRecursive(Gridview1, "DropDownList2");

            if (((DropDownList)sender).SelectedValue == "1")
            {
                ddlzz.Items.Add(new ListItem("aaa", "10"));
                ddlzz.Items.Add(new ListItem("aaaa", "100"));
            }
            else if (((DropDownList)sender).SelectedValue == "2")
            {
                ddlzz.Items.Add(new ListItem("bbb", "20"));
                ddlzz.Items.Add(new ListItem("bbbb", "200"));
            }
        }



这里的主要思想是方法FindControlRecursive,其工作方式如下:



The main idea here being the method FindControlRecursive that works as follows:

private Control FindControlRecursive(Control root, string controlid)
        {
            if (root.ID == controlid)
            {
                return root;
            }
            else
            {
                foreach (Control ctrl in root.Controls)
                {
                    Control ctrl2return = FindControlRecursive(ctrl, controlid);

                    if (ctrl2return != null)
                    {
                        return ctrl2return;
                    }
                }

                return null;
            }
        }



您以递归方式进行操作,以找到第二个下拉列表,该列表包含在网格视图中,作为另一个模板列,而第一个下拉列表将没有直接的标识方式.

从GridView中删除onselectedIndexChanged事件,即当您在网格中没有选择按钮时.

您对DropDownList1的标记如下所示:



You do it recursively to find the second drop downlist which is contained in the grid view as another template column and the first dropdown would have no direct way of identifying it.

Remove the onselectedIndexChanged event from the GridView, that is for when you have a select button in your grid which you don''t.

Your markup for the DropDownList1 would look like this:

<asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Change"></asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>



您也可以以相同的方式扩展此逻辑以覆盖第三个下拉列表.希望对您有所帮助.



You can extend this logic to cover the third drop down as well in the same manner. Hope this helps.


尝试一下

Try this

protected void MainCategory_SelectedIndexChanged(object sender, EventArgs e)
   {
//find the SubCategory dropdown and bind it according to the MainCategoryID
   }



谢谢



Thanks


这篇关于如何在GridView中对多个下拉列表值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆