简体   繁体   中英

Using the Footer Template for add new rows in GridView in c#

I am using the Footer Template for add new rows in GridView.

In the Footer Template I have three DropDownList.

In the DropDownList DZ_DDL I have this values:

ZA
ZF
ZG
ZL
ZM
ZVR
ZTR

In the DropDownList M_DDL I have this values:

Z
ZVR
ZTR

I need to check this:

  • If in the dropdownlist DZ_DDL selected value are ZA or ZF or ZG or ZL or ZM I need disabled in the dropdownlist M_DDL the values ZVR and ZTR.
  • If in the dropdownlist DZ_DDL selected value is ZVR I need disabled in the dropdownlist M_DDL the values Z and ZTR.
  • If in the dropdownlist DZ_DDL selected value is ZTR I need disabled in the dropdownlist M_DDL the values Z and ZVR.

Can you explain how do this ?

My code below:

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();
}

ASPX CODE

<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)
        {

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM