Mr A
Mr A

Reputation: 6768

Working with dropdown list & Validation in asp.net

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

Upvotes: 0

Views: 2743

Answers (1)

asd
asd

Reputation:

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

Upvotes: 1

Related Questions