我正在使用页脚模板在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();
}
最佳答案
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代码
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)
{
}
}
关于c# - 使用页脚模板在C#中的GridView中添加新行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25546676/