Sunday, July 21, 2013

How to Create Controls Dynamically in code behind files and Save in database using asp.net

Referred URL - http://www.aspdotnet-suresh.com/2010/09/how-to-create-controls-dynamically-in.html

Introduction:

Here I will explain how to generate the controls dynamically in code behind using asp.net

Description:

Hi here I will explain how to generate the controls dynamically in asp.net code behind
First design the your aspx page with one panel like this 


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>


After that write the following code in page load method like this 


protected void Page_Load(object sender, EventArgs e)
{
Table tbldynamic = new Table();
for (int i = 0; i < 5; i++)
{
TableCell tc = new TableCell();
TableRow tr = new TableRow();
CheckBox _checkbox = new CheckBox();
_checkbox.ID = "chkDynamicCheckBox" + i;
_checkbox.Text = "Checkbox" + i;
tc.Controls.Add(_checkbox);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
}
Panel1.Controls.Add(tbldynamic);
}


Now run application you will see dynamically generated checkboxes like that we can generate controls dynamically.
 
To implement this concept first design table in database and give name as UserDetails as shown below

Column Name
Data Type
Allow Nulls
UserId
int(set identity property=true)
No
UserName
varchar(50)
Yes
Email
varchar(100)
Yes
After completion table design write the following code in your aspx page


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Create controls dynamically in asp.net and handle button click events</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:Panel ID="pnlInfo" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
After completion of aspx page design add the following namespaces in code behind

C# code


using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
After add namespaces write the following code in code behind like as shown below


protected void Page_Load(object sender, EventArgs e)
{
Table tbldynamic = new Table();
TableCell tc=new TableCell();
TableCell tc1=new TableCell();
TableRow tr = new TableRow();
Label lblName =new Label();
lblName.ID = "lblName";
lblName.Text = "UserName:";
tc.Controls.Add(lblName);
TextBox txtName=new TextBox();
txtName.ID = "txtName";
tc1.Controls.Add(txtName);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tbldynamic.Rows.Add(tr);
tc=new TableCell();
tc1=new TableCell();
tr=new TableRow();
Label lblEmail = new Label();
lblEmail.ID = "lblEmail";
lblEmail.Text = "Email:";
tc.Controls.Add(lblEmail);
TextBox txtEmail = new TextBox();
txtEmail.ID = "txtEmail";
tc1.Controls.Add(txtEmail);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tbldynamic.Rows.Add(tr);
tc = new TableCell();
tc1 = new TableCell();
tr = new TableRow();
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Submit";
btnSubmit.Click += new System.EventHandler(btnSubmit_click);
tc1.Controls.Add(btnSubmit);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tbldynamic.Rows.Add(tr);
pnlInfo.Controls.Add(tbldynamic);
}
// Dynamic button click event which is used to save values in database
protected void btnSubmit_click(object sender, EventArgs e)
{
String str = string.Empty;
TextBox txtUserName = (TextBox)pnlInfo.FindControl("txtName");
// str = txtUserName.Text;
TextBox txtEmail = (TextBox)pnlInfo.FindControl("txtEmail");
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
using (SqlCommand cmd =new SqlCommand("INSERT INTO UserDetails(UserName,Email) VALUES(@name,@mail)",con))
{
con.Open();
cmd.Parameters.AddWithValue("@name", txtUserName.Text);
cmd.Parameters.AddWithValue("@mail", txtEmail.Text);
cmd.ExecuteNonQuery();
txtEmail.Text = string.Empty;
txtUserName.Text = string.Empty;
ClientScript.RegisterClientScriptBlock(this.GetType(), "btn","<script type = 'text/javascript'>alert('UserDetails saved Successfully');</script>");
}
}
}

VB.NET Code

Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Class Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

Dim tbldynamic As New Table()
Dim tc As New TableCell()
Dim tc1 As New TableCell()
Dim tr As New TableRow()
Dim lblName As New Label()
lblName.ID = "lblName"
lblName.Text = "UserName:"
tc.Controls.Add(lblName)
Dim txtName As New TextBox()
txtName.ID = "txtName"
tc1.Controls.Add(txtName)
tr.Cells.Add(tc)
tr.Cells.Add(tc1)
tbldynamic.Rows.Add(tr)
tc = New TableCell()
tc1 = New TableCell()
tr = New TableRow()
Dim lblEmail As New Label()
lblEmail.ID = "lblEmail"
lblEmail.Text = "Email:"
tc.Controls.Add(lblEmail)
Dim txtEmail As New TextBox()
txtEmail.ID = "txtEmail"
tc1.Controls.Add(txtEmail)
tr.Cells.Add(tc)
tr.Cells.Add(tc1)
tbldynamic.Rows.Add(tr)
tc = New TableCell()
tc1 = New TableCell()
tr = New TableRow()
Dim btnSubmit As New Button()
btnSubmit.ID = "btnSubmit"
btnSubmit.Text = "Submit"
AddHandler btnSubmit.Click, AddressOf btnSubmit_click
tc1.Controls.Add(btnSubmit)
tr.Cells.Add(tc)
tr.Cells.Add(tc1)
tbldynamic.Rows.Add(tr)
pnlInfo.Controls.Add(tbldynamic)
End Sub
' Dynamic button click event which is used to save values in database
Protected Sub btnSubmit_click(ByVal sender As Object, ByVal e As EventArgs)
Dim str As [String] = String.Empty
Dim txtUserName As TextBox = DirectCast(pnlInfo.FindControl("txtName"), TextBox)
' str = txtUserName.Text;
Dim txtEmail As TextBox = DirectCast(pnlInfo.FindControl("txtEmail"), TextBox)
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
Using cmd As New SqlCommand("INSERT INTO UserDetails(UserName,Email) VALUES(@name,@mail)", con)
con.Open()
cmd.Parameters.AddWithValue("@name", txtUserName.Text)
cmd.Parameters.AddWithValue("@mail", txtEmail.Text)
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
Demo


No comments: