我有一个 DropDownList 填充了一组值。当我单击一个值时,DropDownList 值应保持为该值,但大多数情况下它会重置为默认值(选择类型)。
这是 DDL:
<asp:TemplateField HeaderText="Criteria Type:" SortExpression="CriteriaType">
<EditItemTemplate>
<asp:DropDownList ID="CriteriaType" AutoPostBack="True" runat="server" SelectedValue='<%# Bind("CriteriaType") %>'>
<asp:ListItem Value="0">Select a Type</asp:ListItem>
<asp:ListItem Value="TMEL Table">TMEL Table</asp:ListItem>
<asp:ListItem Value="Risk Matrix">Risk Matrix</asp:ListItem>
<asp:ListItem Value="Alarm Rationalization">Alarm Rationalization</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="CriteriaType" AutoPostBack="True" runat="server" ViewStateMode="Enabled"
SelectedValue='<%# Bind("CriteriaType") %>'>
<asp:ListItem Value="0">Select a Type</asp:ListItem>
<asp:ListItem Value="TMEL Table">TMEL Table</asp:ListItem>
<asp:ListItem Value="Risk Matrix">Risk Matrix</asp:ListItem>
<asp:ListItem Value="Alarm Rationalization">Alarm Rationalization</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CriteriaType") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
这是后面的代码:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList criteriaType = (DropDownList)DetailsView1.FindControl("CriteriaType");
if (criteriaType.SelectedValue == "TMEL Table")
{
DetailsView1.Fields[5].Visible = true;
DetailsView1.Fields[6].Visible = false;
DetailsView1.Fields[7].Visible = false;
}
else if ((criteriaType.SelectedValue == "Risk Matrix"))
{
DetailsView1.Fields[5].Visible = true;
DetailsView1.Fields[6].Visible = true;
DetailsView1.Fields[7].Visible = true;
DetailsView1.Fields[8].Visible = true;
}
else if ((criteriaType.SelectedValue == "Alarm Rationalization"))
{
DetailsView1.Fields[5].Visible = true;
DetailsView1.Fields[6].Visible = true;
DetailsView1.Fields[7].Visible = true;
DetailsView1.Fields[8].Visible = true;
}
else if (criteriaType.SelectedValue == "Select a Type")
{
DetailsView1.Fields[5].Visible = false;
DetailsView1.Fields[6].Visible = false;
DetailsView1.Fields[7].Visible = false;
}
}