Hi ,
Check this
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
else
{
if (ViewState["dt"] != null)
{
dt = (DataTable)ViewState["dt"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
DataTable dt = new DataTable();
DataTable search(int id)
{
using (
SqlConnection con =
new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("usp_Search_All", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("id", id);
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
cmd.Dispose();
return dt;
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource =(DataTable)ViewState["dt"];
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
dt = search(Convert.ToInt32(TextBox1.Text));
ViewState.Add("dt", dt);
}
void Bind()
{
using (
SqlConnection con =
new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("usp_Select_All", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
cmd.Dispose();
}
}
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging" PageSize="5">
</asp:GridView>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
Best Regards
M.Mitwalli