<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title></title></head><body><form id="form1" runat="server"><div>Username: <asp:TextBox ID="TextBox1" runat="server"/><br />Sitename: <asp:TextBox ID="TextBox2" runat="server"/><br />Facebook Id: <asp:TextBox ID="TextBox3" runat="server"/><br />Twitter Id: <asp:TextBox ID="TextBox4" runat="server"/><br /><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:GridView ID="RGrid" runat="server" ></asp:GridView></div></form></body></html>
Code for VB.NET:
Imports System.Collections.GenericImports System.LinqImports System.WebImports System.Web.UIImports System.Web.UI.WebControlsImports System.DataPartial Public Class multiGridInherits System.Web.UI.PageProtected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)End SubPrivate Sub BindGrid(ByVal rowcount As Integer)Dim dt As New DataTable()Dim dr As DataRowdt.Columns.Add(New System.Data.DataColumn("UserName", GetType([String])))dt.Columns.Add(New System.Data.DataColumn("Sitename", GetType([String])))dt.Columns.Add(New System.Data.DataColumn("Facebook id", GetType([String])))dt.Columns.Add(New System.Data.DataColumn("Twitter id", GetType([String])))If Session("CurrentData") IsNot Nothing ThenFor i As Integer = 0 To rowcountdt = DirectCast(Session("CurrentData"), DataTable)If dt.Rows.Count > 0 Thendr = dt.NewRow()dr(0) = dt.Rows(0)(0).ToString()dr(1) = dt.Rows(0)(1).ToString()dr(2) = dt.Rows(0)(2).ToString()dr(3) = dt.Rows(0)(3).ToString()End IfNextdr = dt.NewRow()dr(0) = TextBox1.Textdr(1) = TextBox2.Textdr(2) = TextBox3.Textdr(3) = TextBox4.Textdt.Rows.Add(dr)Elsedr = dt.NewRow()dr(0) = TextBox1.Textdr(1) = TextBox2.Textdr(2) = TextBox3.Textdr(3) = TextBox4.Textdt.Rows.Add(dr)End If' If Session has a data then use the value as the DataSourceIf Session("CurrentData") IsNot Nothing ThenRGrid.DataSource = DirectCast(Session("CurrentData"), DataTable)RGrid.DataBind()Else' Bind GridView with the initial data assocaited in the DataTableRGrid.DataSource = dtRGrid.DataBind()End If' Store the DataTable in Session to retain the valuesSession("CurrentData") = dtEnd SubProtected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)' Check if the Session has a data assoiciated within it. IfIf Session("CurrentData") IsNot Nothing ThenDim dt As DataTable = DirectCast(Session("CurrentData"), DataTable)Dim count As Integer = dt.Rows.CountBindGrid(count)ElseBindGrid(1)End IfEnd SubEnd Class
Code for C#.NET:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;public partial class multiGrid : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}private void BindGrid(int rowcount){DataTable dt = new DataTable();DataRow dr;dt.Columns.Add(new System.Data.DataColumn("UserName", typeof(String)));dt.Columns.Add(new System.Data.DataColumn("Sitename", typeof(String)));dt.Columns.Add(new System.Data.DataColumn("Facebook id", typeof(String)));dt.Columns.Add(new System.Data.DataColumn("Twitter id", typeof(String)));if (Session["CurrentData"] != null){for (int i = 0; i < rowcount + 1; i++){dt = (DataTable)Session["CurrentData"];if (dt.Rows.Count > 0){dr = dt.NewRow();dr[0] = dt.Rows[0][0].ToString();dr[1] = dt.Rows[0][1].ToString();dr[2] = dt.Rows[0][2].ToString();dr[3] = dt.Rows[0][3].ToString();}}dr = dt.NewRow();dr[0] = TextBox1.Text;dr[1] = TextBox2.Text;dr[2] = TextBox3.Text;dr[3] = TextBox4.Text;dt.Rows.Add(dr);}else{dr = dt.NewRow();dr[0] = TextBox1.Text;dr[1] = TextBox2.Text;dr[2] = TextBox3.Text;dr[3] = TextBox4.Text;dt.Rows.Add(dr);}// If Session has a data then use the value as the DataSourceif (Session["CurrentData"] != null){RGrid.DataSource = (DataTable)Session["CurrentData"];RGrid.DataBind();}else{// Bind GridView with the initial data assocaited in the DataTableRGrid.DataSource = dt;RGrid.DataBind();}// Store the DataTable in Session to retain the valuesSession["CurrentData"] = dt;}protected void Button1_Click(object sender, EventArgs e){// Check if the Session has a data assoiciated within it. Ifif (Session["CurrentData"] != null){DataTable dt = (DataTable)Session["CurrentData"];int count = dt.Rows.Count;BindGrid(count);}else{BindGrid(1);}}}
Output:
0 comments:
Post a Comment