使用页脚模板在c#中的GridView中添加新行

时间:2014-08-28 10:34:44

标签: c# asp.net

我正在使用页脚模板在GridView中添加新行。

在页脚模板中,我有三个DropDownList。

在DropDownList DZ_DDL中我有这个值:

ZA
ZF
ZG
ZL
ZM
ZVR
ZTR

在DropDownList M_DDL中我有这个值:

Z
ZVR
ZTR

我需要检查一下:

  • 如果在下拉列表DZ_DDL中选择的值是ZA或ZF或ZG或ZL 或ZM我需要在下拉列表M_DDL中禁用值ZVR和 ZTR。
  • 如果在下拉列表DZ_DDL中选择的值是ZVR,我需要禁用 下拉列表M_DDL值Z和ZTR。
  • 如果在下拉列表DZ_DDL中选择的值是ZTR我需要禁用 下拉列表M_DDL值Z和ZVR。

你能解释一下这是怎么回事吗?

我的代码如下:

if (e.Row.RowType == DataControlRowType.Footer)
{
    DropDownList DZ_DDL = (DropDownList)e.Row.FindControl("DZ_DDL");
    DropDownList Level_DDL = (DropDownList)e.Row.FindControl("Level_DDL");
    DropDownList M_DDL = (DropDownList)e.Row.FindControl("M_DDL");

    sql1 = " SELECT ....; ";
    OdbcCommand cmd = new OdbcCommand(sql1);
    DZ_DDL.DataSource = GetData(cmd);
    DZ_DDL.DataTextField = "name";
    DZ_DDL.DataValueField = "name";
    DZ_DDL.DataBind();


    sql2 = " SELECT ....; ";
    OdbcCommand cmd2 = new OdbcCommand(sql2);
    Level_DDL.DataSource = GetData(cmd2);
    Level_DDL.DataTextField = "PZA";
    Level_DDL.DataValueField = "PZA";
    Level_DDL.DataBind();


    sql3 = " SELECT ....; ";
    OdbcCommand cmd3 = new OdbcCommand(sql3);
    M_DDL.DataSource = GetData(cmd3);
    M_DDL.DataTextField = "Name";
    M_DDL.DataValueField = "Name";
    M_DDL.DataBind();
}

1 个答案:

答案 0 :(得分:0)

ASPX代码

<div>
        <asp:ScriptManager ID="SM" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel" runat="server">
            <ContentTemplate>

  <asp:DropDownList ID="DZ_DDL" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DZ_DDL_SelectedIndexChanged" OnTextChanged="DZ_DDL_TextChanged" ViewStateMode="Enabled">
                    <asp:ListItem Value="ZA">ZA</asp:ListItem>
                    <asp:ListItem Value="ZF" Enabled="true">ZF</asp:ListItem>
                    <asp:ListItem Value="ZG">ZG</asp:ListItem>
                    <asp:ListItem Value="ZL">ZL</asp:ListItem>
                    <asp:ListItem Value="ZM">ZM</asp:ListItem>
                    <asp:ListItem Value="ZVR">ZVR</asp:ListItem>
                    <asp:ListItem Value="ZTR">ZTR</asp:ListItem>
                </asp:DropDownList>

                <asp:DropDownList ID="M_DDL" runat="server">
                    <asp:ListItem>Z</asp:ListItem>
                    <asp:ListItem>ZVR</asp:ListItem>
                    <asp:ListItem>ZTR</asp:ListItem>
                </asp:DropDownList>
            </ContentTemplate>
            <Triggers>
                 <asp:AsyncPostBackTrigger ControlID="DZ_DDL" EventName="SelectedIndexChanged" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
CS CODE
 protected void DZ_DDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
                DropDownList DZ_DDL = (DropDownList)this.FindControl("DZ_DDL");
                //DropDownList Level_DDL = (DropDownList)this.FindControl("Level_DDL");
                DropDownList M_DDL = (DropDownList)this.FindControl("M_DDL");

                string str = DZ_DDL.SelectedValue.ToString();
                if (str == "ZA" || str == "ZF" || str == "ZG" || str == "ZL" || str == "ZL")
                {
                    ListItem i = M_DDL.Items.FindByValue("ZVR");
                    i.Attributes.Add("style", "color:gray;");
                    i.Attributes.Add("disabled", "true");

                    i = M_DDL.Items.FindByValue("ZTR");
                    i.Attributes.Add("style", "color:gray;");
                    i.Attributes.Add("disabled", "true");
                }
                else if (str == "ZVR")
                {
                    ListItem i = M_DDL.Items.FindByValue("Z");
                    i.Attributes.Add("style", "color:gray;");
                    i.Attributes.Add("disabled", "true");

                    i = M_DDL.Items.FindByValue("ZTR");
                    i.Attributes.Add("style", "color:gray;");
                    i.Attributes.Add("disabled", "true");
                }

                else if (str == "ZTR")
                {
                    ListItem i = M_DDL.Items.FindByValue("Z");
                    i.Attributes.Add("style", "color:gray;");
                    i.Attributes.Add("disabled", "true");

                    i = M_DDL.Items.FindByValue("ZVR");
                    i.Attributes.Add("style", "color:gray;");
                    i.Attributes.Add("disabled", "true");
                }
            }
        catch(Exception ex)
        {

        }
    }