Hi slb2013,
Please refer below sample.
HTML
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server">
<table>
<tr>
<td>
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" Width="300px"
Visible="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</td>
<td>
<asp:Button ID="btnA" runat="server" Text="ShowGridViewA" OnClick="OnShow_A" />
</td>
<td>
<asp:TextBox ID="txtName" runat="server" /><br />
<asp:TextBox ID="txtCountry" runat="server" /><br />
<asp:Button ID="btnEnterA" runat="server" Text="Add_A" OnClick="OnAdd_A" />
</td>
<td>
<asp:Button ID="btnAR" runat="server" Text="RefreshA" OnClick="OnRefresh_A" />
</td>
</tr>
</table>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel2" runat="server">
<table>
<tr>
<td>
<asp:GridView ID="gvCustomers_Country" runat="server" AutoGenerateColumns="false"
Width="300px" Visible="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="CustomersId" HeaderText="CustomersId" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</td>
<td>
<asp:Button ID="btnB" runat="server" Text="ShowGridViewB" OnClick="OnShow_B" />
</td>
<td>
<asp:TextBox ID="txtCustomersId" runat="server" /><br />
<asp:TextBox ID="txtCountry2" runat="server" /><br />
<asp:Button ID="btnEnterB" runat="server" Text="Add_B" OnClick="OnAdd_B" />
</td>
<td>
<asp:Button ID="btnBR" runat="server" Text="RefreshB" OnClick="OnRefresh_B" />
</td>
</tr>
</table>
</asp:Panel>
<br />
<asp:Button ID="btnC" runat="server" Text="ButtonC" OnClick="OnClickC" />
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
}
protected void OnShow_A(object sender, EventArgs e)
{
this.BindGridView_A();
gvCustomers.Visible = true;
}
protected void OnShow_B(object sender, EventArgs e)
{
this.BindgridView_B();
gvCustomers_Country.Visible = true;
}
protected void OnAdd_A(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers (Name,Country) VALUES (@Name,@Country)", con))
{
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtName.Text = string.Empty;
txtCountry.Text = string.Empty;
}
}
this.BindGridView_A();
}
protected void OnAdd_B(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers_Country (CustomersId,Country) VALUES (@CustomersId,@Country)", con))
{
cmd.Parameters.AddWithValue("@CustomersId", txtCustomersId.Text.Trim());
cmd.Parameters.AddWithValue("@Country", txtCountry2.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtCustomersId.Text = string.Empty;
txtCountry2.Text = string.Empty;
}
}
this.BindgridView_B();
}
protected void OnRefresh_A(object sender, EventArgs e)
{
gvCustomers.Visible = true;
this.BindGridView_A();
}
protected void OnRefresh_B(object sender, EventArgs e)
{
gvCustomers_Country.Visible = true;
this.BindgridView_B();
}
protected void OnClickC(object sender, EventArgs e)
{
gvCustomers.Visible = true;
this.BindGridView_A();
gvCustomers_Country.Visible = true;
this.BindgridView_B();
}
private void BindGridView_A()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
con.Open();
gvCustomers.DataSource = cmd.ExecuteReader();
gvCustomers.DataBind();
con.Close();
}
}
}
private void BindgridView_B()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers_Country", con))
{
con.Open();
gvCustomers_Country.DataSource = cmd.ExecuteReader();
gvCustomers_Country.DataBind();
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub OnShow_A(ByVal sender As Object, ByVal e As EventArgs)
Me.BindGridView_A()
gvCustomers.Visible = True
End Sub
Protected Sub OnShow_B(ByVal sender As Object, ByVal e As EventArgs)
Me.BindgridView_B()
gvCustomers_Country.Visible = True
End Sub
Protected Sub OnAdd_A(ByVal sender As Object, ByVal e As EventArgs)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Customers (Name,Country) VALUES (@Name,@Country)", con)
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim())
cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim())
con.Open()
cmd.ExecuteNonQuery()
con.Close()
txtName.Text = String.Empty
txtCountry.Text = String.Empty
End Using
End Using
Me.BindGridView_A()
End Sub
Protected Sub OnAdd_B(ByVal sender As Object, ByVal e As EventArgs)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Customers_Country (CustomersId,Country) VALUES (@CustomersId,@Country)", con)
cmd.Parameters.AddWithValue("@CustomersId", txtCustomersId.Text.Trim())
cmd.Parameters.AddWithValue("@Country", txtCountry2.Text.Trim())
con.Open()
cmd.ExecuteNonQuery()
con.Close()
txtCustomersId.Text = String.Empty
txtCountry2.Text = String.Empty
End Using
End Using
Me.BindgridView_B()
End Sub
Protected Sub OnRefresh_A(ByVal sender As Object, ByVal e As EventArgs)
gvCustomers.Visible = True
Me.BindGridView_A()
End Sub
Protected Sub OnRefresh_B(ByVal sender As Object, ByVal e As EventArgs)
gvCustomers_Country.Visible = True
Me.BindgridView_B()
End Sub
Protected Sub OnClickC(ByVal sender As Object, ByVal e As EventArgs)
gvCustomers.Visible = True
Me.BindGridView_A()
gvCustomers_Country.Visible = True
Me.BindgridView_B()
End Sub
Private Sub BindGridView_A()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
con.Open()
gvCustomers.DataSource = cmd.ExecuteReader()
gvCustomers.DataBind()
con.Close()
End Using
End Using
End Sub
Screenshot