使用下拉列表和在 asp.net 中验证

发布于 2024-11-07 08:02:25 字数 1268 浏览 1 评论 0原文

我有一个下拉列表,其中填充了产品表中的产品。我有一个名为“添加产品”的按钮,因此当用户想要添加新产品而不是从下拉列表中选择产品时,应该单击“添加新产品”,然后面板 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();
        }
    }

I have got a dropdown list populated with products from a product table. I have a button named ADD Product, so when the user wants to add a new product instead of selecting product from the drop down list, one should click on add new then panel 1 should show which includes a textbox. Panel1 visibility is set to false by default and it should be visible when the user clicks on the button. Also I want a validation on dropdown list and textbox(if panel is visible). Below is the code which is not working :

   <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 />

codebehind:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原野 2024-11-14 08:02:25

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

<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" />

You can use this concept for validation on dropdownlist. I am given you an example please check this...

<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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文