Editable MultiView Using TextBoxes In ASP.NET

In this tutorial, we will guide you through another approach for an editable MultiView in an ASP.NET, using C#, where I had taken three views inside MultiView. In the first view, we will take the name and city from the user and on the second view; we will make a view to edit the previous details and on the third view, we will show all the details in the gridcontrol.

INITIAL CHAMBER

Step1

Open Your Visual Studio 2010 and create an empty Website. Give a suitable name [data_demo].

Step2

In Solution Explorer, you get your empty Website. Add a Web form, SQL database. Proceed, as shown below.

For Web Form

data_demo (Your Empty Website) -> Right click on Add New Item -> Web Form. Name it as -> data_demo.aspx.

For SQL Server database

data_demo (Your empty Website) -> Right Click on Add New Item -> SQL Server database. [Add the database inside the App_Data_folder].

DATABASE CHAMBER

Step3

Get to your database [Database.mdf], we will create a table - -> tbl_datas1. Go to the database.mdf - -> Table - -> Add New table and design your table, as shown below.

ASP.NET

DESIGN CHAMBER

Step4

Open your data_demo.aspx file to create the design. We will drag a Mutiview Control. Inside it, take three views and add an appropriate control, as shown below.

Here, inside the third view, we had taken GridView control to show all the details about the student data. If you find any difficulty in making this design, I had also shared the code too. You can have a look at it.

ASP.NET

CODE CHAMBER

Step5

Open data_demo.aspx.cs file to write the code for making editable MultiView. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9. using System.Data.Sql;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Web.UI.HtmlControls;  
  12.   
  13. public partial class Multiview : System.Web.UI.Page  
  14. {  
  15.     string Name = "";  
  16.     string City = "";  
  17.    static int id = 0;  
  18.     protected void Page_Load(object sender, EventArgs e)  
  19.     {  
  20.         if (!IsPostBack)  
  21.         {  
  22.             MultiView1.ActiveViewIndex = 0;  
  23.         }  
  24.   
  25.     }  
  26.   
  27.     protected void Button2_Click(object sender, EventArgs e)  
  28.     {  
  29.         
  30.         SqlConnection con = new SqlConnection(@"Data Source=Nilesh;Initial Catalog=test_db;Integrated Security=True");  
  31.         SqlCommand cmd = new SqlCommand("insert into tbl_datas1 values(@name,@city)", con);  
  32.         cmd.Parameters.AddWithValue("@name", txt_Name.Text);  
  33.         cmd.Parameters.AddWithValue("@city", txt_City.Text);  
  34.         con.Open();  
  35.         cmd.ExecuteNonQuery();  
  36.         con.Close();  
  37.         MultiView1.ActiveViewIndex = 1;  
  38.     }  
  39.   
  40.   
  41.   
  42.     protected void Button4_Click(object sender, EventArgs e)  
  43.     {  
  44.         if (txt_Name_Edit.Text.Trim() == "")  
  45.         {  
  46.             lbl_Name_Val_Edit.Text = "Enter Student Name.";  
  47.             txt_Name_Edit.Focus();  
  48.             return;  
  49.         }  
  50.         if (txt_City_Edit.Text.Trim() == "")  
  51.         {  
  52.             lbl_City_Val_Edit.Text = "Enter Student City.";  
  53.             txt_City_Edit.Focus();  
  54.             return;  
  55.         }  
  56.   
  57.         SqlConnection con = new SqlConnection(@"Data Source=Nilesh;Initial Catalog=test_db;Integrated Security=True");  
  58.         SqlCommand cmd = new SqlCommand("update tbl_datas1 set name= @name,city= @city where id = " + id.ToString(), con);  
  59.         cmd.Parameters.AddWithValue("@name", txt_Name_Edit.Text);  
  60.         cmd.Parameters.AddWithValue("@city", txt_City_Edit.Text);  
  61.         con.Open();  
  62.         cmd.ExecuteNonQuery();  
  63.         con.Close();  
  64.         MultiView1.ActiveViewIndex = 2;  
  65.   
  66.   
  67.   
  68.         SqlCommand cmd1 = new SqlCommand("select * from tbl_datas1", con);  
  69.         SqlDataAdapter sda = new SqlDataAdapter(cmd1);  
  70.         DataTable dt = new DataTable();  
  71.         sda.Fill(dt);  
  72.         GridView1.DataSource = dt;  
  73.         GridView1.DataBind();  
  74.   
  75.               
  76.   
  77.     }  
  78.   
  79.     protected void LinkButton3_Click(object sender, EventArgs e)  
  80.     {  
  81.         txt_Name_Edit.ReadOnly = true;  
  82.         txt_City_Edit.ReadOnly = true;  
  83.   
  84.         txt_Name_Edit.Text = Name;  
  85.         txt_City_Edit.Text = City;  
  86.     }  
  87.   
  88.     protected void txt_Name_TextChanged(object sender, EventArgs e)  
  89.     {  
  90.         if (MultiView1.ActiveViewIndex == 0)  
  91.             if (txt_Name.Text.Trim() != "")  
  92.             {  
  93.                 lbl_Name_Val.Text = "";  
  94.             }  
  95.   
  96.         if (MultiView1.ActiveViewIndex == 1)  
  97.             if (txt_Name_Edit.Text.Trim() != "")  
  98.             {  
  99.                 lbl_Name_Val_Edit.Text = "";  
  100.             }  
  101.     }  
  102.   
  103.     protected void txt_City_TextChanged(object sender, EventArgs e)  
  104.     {  
  105.         if (MultiView1.ActiveViewIndex == 0)  
  106.             if (txt_City.Text.Trim() != "")  
  107.             {  
  108.                 lbl_City_Val.Text = "";  
  109.             }  
  110.   
  111.         if (MultiView1.ActiveViewIndex == 1)  
  112.             if (txt_City_Edit.Text.Trim() != "")  
  113.             {  
  114.                 lbl_City_Val_Edit.Text = "";  
  115.             }  
  116.     }  
  117.   
  118.     protected void View2_Activate(object sender, EventArgs e)  
  119.     {  
  120.         if (MultiView1.ActiveViewIndex != 1) return;  
  121.   
  122.         SqlConnection con = new SqlConnection(@"Data Source=Nilesh;Initial Catalog=test_db;Integrated Security=True");  
  123.   
  124.         SqlCommand cmd = new SqlCommand("select TOP(1) id, name, city from tbl_datas1 order by id DESC", con);  
  125.         SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  126.         DataSet ds = new DataSet();  
  127.         sda.Fill(ds);  
  128.         con.Open();  
  129.   
  130.           
  131.             Name = txt_Name_Edit.Text = ds.Tables[0].Rows[0]["name"].ToString();  
  132.             City = txt_City_Edit.Text = ds.Tables[0].Rows[0]["city"].ToString();  
  133.   
  134.             id = Convert.ToInt32(ds.Tables[0].Rows[0]["id"]);  
  135.   
  136.             txt_Name_Edit.ReadOnly = true;  
  137.             txt_City_Edit.ReadOnly = true;  
  138.           
  139.         con.Close();  
  140.   
  141.     }  
  142.   
  143.     protected void LinkButton1_Click(object sender, EventArgs e)  
  144.     {  
  145.       
  146.         txt_Name_Edit.ReadOnly = false;  
  147.         txt_City_Edit.ReadOnly = false;  
  148.     }  
  149. }   

OUTPUT CHAMBER

Here, we will insert name and city from the first view and see is it saving in the database or not?

First View

ASP.NET

Tbl_datas1 - Name and City

ASP.NET

Second View

ASP.NET

Here, in the second view, the data has come from first view, so that we can edit it. I am updating my city here as DELHI.

Check this in the database to see whether it is stored properly or not. 

ASP.NET

Check also in Gridview on third view

ASP.NET

It's perfect. I am sorry for the dummy data in the GridView. I have tested it so many times due to which it is showing like this, otherwise it's working  fine.

I hope you like it. Thank you for reading. Have a good day.