Harish M

Harish M

  • NA
  • 6
  • 10.2k

Load more data records on button click in Asp.Net Repeater

Aug 26 2014 6:35 AM
Hi Friends,
 
    Am using ASP.Net framework 2.0, my problem is when i load data to repeater control from database the whole web page is refreshing.  "Load More Button" is used to retrieve more data from database.  I need only repeater control should be refreshed when i click "Load More Button".
 
 Here is the code,
 
aspx file
 
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
num = 4;
ViewState["num"] = num;
BindRepeater(num);
}
 
public void BindRepeater(int numOfRows)
{
DataTable dt = new DataTable();
SqlCommand cmd = null;
SqlDataAdapter adp = null;
try
{
int rCount = rowCount();
if (numOfRows > rCount)
{
btnLoadMore.Visible = false;
}
cmd = new SqlCommand("GetStudentDetails_SP", con);
cmd.Parameters.AddWithValue("@topVal", numOfRows);
cmd.CommandType = CommandType.StoredProcedure;
adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
rptStudentDetails.DataSource = dt;
rptStudentDetails.DataBind();
}
else
{
rptStudentDetails.DataSource = null;
rptStudentDetails.DataBind();
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Oops!! Error occured: " + ex.Message.ToString() + "');", true);
}
finally
{
con.Close();
cmd.Dispose();
adp = null;
dt.Clear();
dt.Dispose();
}
}
protected int rowCount()
{
int NoOfRows = 0;
SqlCommand cmd = new SqlCommand("GetStudentDetailsCount_SP", con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
con.Open();
NoOfRows = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Oops!! Error occured: " + ex.Message.ToString() + "');", true);
}
finally
{
con.Close();
cmd.Dispose();
}
return NoOfRows;
}
protected void btnLoadMore_Click(object sender, EventArgs e)
{
int numVal = Convert.ToInt32(ViewState["num"]) + 4;
BindRepeater(numVal);
ViewState["num"] = numVal;
}
 
html file 
  
<div style="position:relative; margin-top:1px;"> 
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<fieldset style="width:100%;">
         <asp:Repeater runat="server" ID="rptStudentDetails">
<HeaderTemplate>
</HeaderTemplate>
 
<ItemTemplate>
<div class="single-product">
<a href=""><img src="images/p1.png" alt="Item Image"/></a>
<p class="productname"><a><%#Eval("SM_NAME") %></a></p>
<asp:Button ID="Button1" runat="server" Text="View All" Height="24" Width="75" BackColor="#50A748" ForeColor="#FFFFFF" CssClass="product_button" />
</div>
</ItemTemplate>
 
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</fieldset>
<div style="width: 59%; height: 50px; padding-top: 10px; padding-left: 42%; float: left;">
<asp:Button ID="btnLoadMore" runat="server" Text="Load More Data"
onclick="btnLoadMore_Click" />
<asp:UpdateProgress runat="server" ID="UpdateProgress1" DisplayAfter="10">
<ProgressTemplate>
<img src="images/loading.gif" alt="wait image" />
</ProgressTemplate>
</asp:UpdateProgress>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div> 
 
 
Please help me to solve my problem 
 
 

Answers (4)