Saturday 18 August 2012

How to create button Dynamically and save data into database in asp.net

Here we learn how to create button dynamically means at run time.

Step1:

First create table in database  as below
create table loginDetails(user_name varchar(50),password varchar(50)).

Step2:

For this open VS 2010> go to file menu> open new website > give it to name>click ok.

DEsign of aspx like below:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicButton.aspx.cs" Inherits="DynamicControlCreation_DynamicButton" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Create button dynamically and store data in database dynamically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server">
        </asp:Panel>
    </div>
    </form>
</body>
</html>

Code for code behind file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class DynamicControlCreation_DynamicButton : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Table tb = new Table();
        TableCell tc = new TableCell();
        TableCell tc1 = new TableCell();
        TableRow tr = new TableRow();
        Label lb = new Label();
        lb.ID = "username";
        lb.Text = "User Name";
        tc.Controls.Add(lb);
        TextBox txtBox = new TextBox();
        txtBox.ID = "txt_username";
        tc1.Controls.Add(txtBox);
        tr.Cells.Add(tc);
        tr.Cells.Add(tc1);
        tb.Rows.Add(tr);
        Label lb1 = new Label();
        lb1.ID = "password";
        lb1.Text = "Password";
        tc1.Controls.Add(lb1);
        TextBox txtBox1 = new TextBox();
        txtBox1.ID = "txt_pass";
        tc1.Controls.Add(txtBox1);
        tr.Cells.Add(tc);
        tr.Cells.Add(tc1);
        tb.Rows.Add(tr);
        Button btnsubmit = new Button();
        btnsubmit.ID = "btnsubmit";
        btnsubmit.Text = "Submit";
        btnsubmit.Click += new EventHandler(btnsubmit_Click);
        tc1.Controls.Add(btnsubmit);
        tr.Cells.Add(tc);
        tr.Cells.Add(tc1);
        tb.Rows.Add(tr);
        Panel1.Controls.Add(tb);
    }

   protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string str = string.Empty;
        TextBox txtusername = (TextBox)Panel1.FindControl("txt_username");
        TextBox txtpassword = (TextBox)Panel1.FindControl("txt_pass");
        using (SqlConnection con = new SqlConnection("Data Source=SUSHIL-70F1F267;Initial Catalog=sushil;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO loginDetails(user_name,password) VALUES(@user_name,@password)", con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@user_name", txtusername.Text);
                cmd.Parameters.AddWithValue("@password", txtpassword.Text);
                cmd.ExecuteNonQuery();
                txtpassword.Text = string.Empty;
                txtusername.Text = string.Empty;
                ClientScript.RegisterClientScriptBlock(this.GetType(), "btn", "<script type = 'text/javascript'>alert('UserDetails saved Successfully');</script>");
            }
        }
    }
   
}

Run the code . Hope this will work perfectly.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.