Saturday, February 26, 2011

Validations for dynamic controls in asp net with C#

Hi Friends I am already explained in my previous post how to create a Dynamic Controls in asp net (Click Here).
Now i am going to explain you how to kept validations for dynamic controls
First open the Microsoft visual studio 2008

Next, select one ASP net web application
Next, open the Default.aspx page
Next, drag and drop one Drop down list from the toolbox
Next, change the name of the dropdown list and add some values
Now the dropdown list will be look like this



<asp:DropDownList ID="ddl_list" runat="server" AutoPostBack="True"
                        onselectedindexchanged="ddl_list_SelectedIndexChanged" Width="100px">
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                        <asp:ListItem>4</asp:ListItem>
                        <asp:ListItem>5</asp:ListItem>
                        <asp:ListItem></asp:ListItem>
</asp:DropDownList>


Now, Open the Default.aspx.cs file and write the code
protected void ddl_list_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_list.SelectedValue != string.Empty)
        {
            for (int i = 0; i < Convert.ToInt32(ddl_list.SelectedValue); i++)
            {
                TextBox txt = new TextBox();
                txt.ID = "txt" + i.ToString();
                PlaceHolder2.Controls.Add(txt);
            }
        }
    }
 
From the above code, we will created only a Dynamic textboxes, now we are going kept the validations for that textboxes
protected void ddl_list_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_list.SelectedValue != string.Empty)
        {
            for (int i = 0; i < Convert.ToInt32(ddl_list.SelectedValue); i++)
            {
                TextBox txt = new TextBox();
                txt.ID = "txt" + i.ToString();
                PlaceHolder2.Controls.Add(txt);
            RequiredFieldValidator req = new RequiredFieldValidator();
            req.ID = "RequiredValidator" + i;
            req.ControlToValidate = "txtBox" + i;
            req.ErrorMessage = "Not Empty";
            req.SetFocusOnError = true;
            PlaceHolder1.Controls.Add(req);

            }
        }
    }

That’s it friends..In this way,we can kept validations for all the controls.
If you have any queries please let me know.i am always ready to share my knowledge with you..
developercode
About the Author
Sayyad is a Software Engineer, Blogger and Founder of Developers Code from India.His articles mainly focus on .Net (Web and Windows based applications), Mobile Technologies (Android,IPhone,BlackBerry) and SEO.

Labels: , , , ,

2 Comments:

At September 3, 2011 at 3:10 PM , Anonymous Anonymous said...

great post...

 
At September 3, 2011 at 3:11 PM , Anonymous Anonymous said...

Nice code ...Thanks

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home