DropDownList值重置

时间:2013-04-29 15:49:04

标签: c# asp.net drop-down-menu

我有一个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;
        }
    }

2 个答案:

答案 0 :(得分:2)

确保添加检查是否不回发

   protected void Page_Load(object sender, EventArgs e)
{
       if(!IsPostBack)
       {
        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;
        }
    }
  }

答案 1 :(得分:0)

您知道IsPostBack事件的工作原理和Page状态吗?基本上,当IsPostBack事件工作时,控件会刷新。

每次从代码隐藏更新页面时,都会触发一个神奇的小Javascript,它会通过XMLHTTTP帖子(我认为)点击服务器,或者真正的硬回帖击中服务器,就像表单帖子一样。因此,当服务器看到它检查IsPostBack标志时,它就像一个自动字段,添加到每个ASP.net页面。

然后检查IsPostBack标志以更改事物的状态。

如果在IsPostBack = true中填充列表框,则它似乎不可选。如果在没有它的情况下填充ListBox,则会显示值不断变化。

这可能就像在PostBack中设置选择值一样简单。

HTH