【问题标题】:Working with dropdown list & Validation in asp.net在 asp.net 中使用下拉列表和验证
【发布时间】:2011-05-16 11:48:19
【问题描述】:

我有一个下拉列表,其中包含产品表中的产品。我有一个名为添加产品的按钮,因此当用户想要添加新产品而不是从下拉列表中选择产品时,应该单击添加新的,然后面板 1 应该显示其中包含一个文本框。 Panel1 可见性默认设置为 false,当用户单击按钮时它应该可见。我还想要对下拉列表和文本框进行验证(如果面板可见)。以下是不工作的代码:

   <asp:DropDownList ID="DropDownList1" runat="server">
         </asp:DropDownList>
         <%--<asp:TextBox ID="txtMake" runat="server" CssClass="txtStyle"></asp:TextBox>--%>
         <asp:Button ID="btnAdd" runat="server" />
         <asp:Panel ID="panel1" Visible="false" runat="server"><asp:TextBox ID="txtAddnew"></asp:TextBox></asp:Panel>
         <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Select a Product" ValidationGroup="insert" ControlToValidate="DropDownList1" ForeColor="Red"></asp:RequiredFieldValidator><br />

代码隐藏:

protected void btnAdd_Click(object sender, EventArgs e)
    {
        panel1.Visible = true;
    }
     protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;
             DropDownList1.DataSource = caravans.GetProductNames();
            DropDownList1.DataBind();
        }
    }

【问题讨论】:

  • 发布您的完整表单设计。

标签: asp.net validation drop-down-menu panel


【解决方案1】:

您可以使用此概念对下拉列表进行验证。我给你一个例子请检查这个...

<asp:DropDownList runat="server" ID="ddl">
            <asp:ListItem Value="-1" Text="Select"></asp:ListItem>
            <asp:ListItem Value="1" Text="One"></asp:ListItem>
            <asp:ListItem Value="2" Text="Two"></asp:ListItem>
        </asp:DropDownList>
        <asp:RegularExpressionValidator ID="reg1" runat="server" ControlToValidate="ddl"
            Display="Dynamic" ErrorMessage="Select Proper" SetFocusOnError="true" Text="Select Proper"
            ValidationGroup="check" ValidationExpression="^\d{0,5}$" />
        <asp:Button ID="btn" runat="server" ValidationGroup="check" Text="Submit" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    相关资源
    最近更新 更多