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