Search & Paging using Repeater control.

R

ratnakarp

Hi,

I have a search text box. The user enters the value in the text box and
click on enter button. In code behind on button click i'm writing the
code to get the values from the database and binding it to a repeater
control. This repeater control has multiple text boxes and buttons. Can
you please tell me how can i do paging in this case ?

I'm posting my code below. The problem is that if i click on
"AdjustThisAd" button, it opens another rebate.aspx page. The
rebate.aspx page also has a button. If i click on that button, the
rebate.aspx closes by itself and refreshes the parent page. Upon
refreshing the page, the repeater control looses the binding to the
dataset and the page doesnt show any record. How can i solve this
problem? Can you please tell me where i'm going wrong?




--------------------------------------------------------------
SearchAds.aspx
----------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="SearchAds.aspx.cs" Inherits="SearchAds" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language ="javascript" type="text/javascript">
var SearchRepCode="<table bgcolor=\"#cccccc\" style=\"width:
541px\">";
SearchRepCode=SearchRepCode+"<tr>";
SearchRepCode=SearchRepCode+"<td style=\"width: 430px; height:
11px\">";
SearchRepCode=SearchRepCode+"Sales Rep:</td>";
SearchRepCode=SearchRepCode+"<td style=\"width: 157px; height:
11px\">";
SearchRepCode=SearchRepCode+"<input id=\"salesrep\"
name=\"salesrep\" onblur=\"SetValues(1)\"style=\"width: 99px\"
type=\"text\" /></td>";
SearchRepCode=SearchRepCode+"<td style=\"width: 437px; height:
11px\">";
SearchRepCode=SearchRepCode+"From Date</td>";
SearchRepCode=SearchRepCode+"<td style=\"width: 81px; height:
11px\">";
SearchRepCode=SearchRepCode+"<input id=\"fromdate\"
name=\"fromdate\" onblur=\"SetValues(2)\" style=\"width: 100px\"
type=\"text\" /></td>";
SearchRepCode=SearchRepCode+"<td style=\"width: 283px; height:
11px\">";
SearchRepCode=SearchRepCode+"To Date</td>";
SearchRepCode=SearchRepCode+"<td style=\"width: 106px; height:
11px\">";
SearchRepCode=SearchRepCode+"<input id=\"todate\"
name=\"todate\" onblur=\"SetValues(3)\" style=\"width: 88px\"
type=\"text\" /></td>";
SearchRepCode=SearchRepCode+"</tr>";
SearchRepCode=SearchRepCode+"</table>";
var SearchAdCode="<table bgcolor=\"#cccccc\" style=\"width:
295px\">";
SearchAdCode=SearchAdCode+"<tr>";
SearchAdCode=SearchAdCode+"<td style=\"width: 130px\">";
SearchAdCode=SearchAdCode+"Ad (PBS) Number</td>";
SearchAdCode=SearchAdCode+"<td style=\"width: 4px\">";
SearchAdCode=SearchAdCode+"<input id=\"adnum\" style=\"width:
112px\" name=\"adnum\" onblur=\"SetValues(4)\" type=\"text\" /></td>";
SearchAdCode=SearchAdCode+"</tr>";
SearchAdCode=SearchAdCode+"</table>";

function SearchClick(value)
{
if(value.indexOf("SalesRep") != -1)
{
var view = document.getElementById('searchView');
view.innerHTML=SearchRepCode;
//document.getElementById('rebateTotal').value =
document.getElementById("OriginalAmount").value;
}
else if(value.indexOf("SearchAd") != -1)
{
var view = document.getElementById('searchView');
view.innerHTML=SearchAdCode;
//document.getElementById('billingTotal').value =
document.getElementById("OriginalAmount").value;
}
}
function SetValues(val) // it take a parameter (1,2,3) 1-
Rebate; 2- BillingAdjustment; 3-Makegood
{
var salesRep
var fromDate;
var toDate;
var adnum;

salesRep = document.getElementById('salesrep').value;
fromdate = document.getElementById('fromdate').value;
todate = document.getElementById('todate').value;
adnum = document.getElementById('adnum').value;
document.getElementById("SalesRep").value =
salesRep;
document.getElementById("FromDate").value = fromdate;
document.getElementById("ToDate").value = todate;
document.getElementById("AdNum").value = adnum;

}
function ChangePage(id)
{
// save the page clicked
document.all.PageNumber.value = id;
// call the __doPostBack function to post back the form and execute
the PageClick event
__doPostBack('PageClick','');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table bgcolor="mediumseagreen" style="width: 638px">
<tr>
<td style="width: 212px">
<input id="Radio1" type="radio" name="Search" value
="SalesRep" runat =server onclick ="SearchClick(this.value)" />
Search by Sales Rep</td>
<td>
<input id="Radio2" type="radio" name="Search"
value="SearchAd" runat =server onclick ="SearchClick(this.value)" />
Search by Ad</td>
</tr>
</table>
<br />
<div id="searchView">
</div>
<!-- <table style="width: 541px">
<tr>
<td style="width: 430px; height: 11px">
Sales Rep:</td>
<td style="width: 157px; height: 11px">
<input id="Text1" style="width: 99px" type="text"
/></td>
<td style="width: 437px; height: 11px">
From Date</td>
<td style="width: 81px; height: 11px">
<input id="Text2" style="width: 100px" type="text"
/></td>
<td style="width: 283px; height: 11px">
To Date</td>
<td style="width: 106px; height: 11px">
<input id="Text3" style="width: 88px" type="text"
/></td>
</tr>
</table>-->
<!-- <table style="width: 295px">
<tr>
<td style="width: 13px">
Ad (PBS) Number</td>
<td style="width: 4px">
<input id="Text4" style="width: 112px" type="text"
/></td>
</tr>
</table>-->
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Enter" /><br />
<input id="SalesRep" runat="server" type="hidden" />
<input id="FromDate" runat="server" type="hidden" />
<input id="ToDate" runat="server" type="hidden" />
<input id="AdNum" runat="server" type="hidden" />
<input type="hidden" runat="server" id="PageNumber" value="1"/>
<input type="hidden" runat="server" id="Pages" value="0"/>
<asp:Repeater ID ="SearchRepeater" runat =server
OnItemCommand="SearchRepeater_ItemCommand"
OnItemDataBound="SearchRepeater_ItemDataBound" >
<ItemTemplate >
<table align="left" rules="none" style="width: 585px; height:
141px" border="0" bgcolor="#cccccc">
<tr>
<td style="width: 161px; height: 15px;">
Account ID</td><td colspan="2" style="height:
15px">
Account Name</td>
<td style="width: 129px; height: 15px;">
<strong>Ad (PBS) &nbsp;Number</strong></td></tr>
<tr>
<td style="width: 161px">
<asp:TextBox ID="custIdtxt" AutoPostBack=true
runat="server" Text ='<%# DataBinder.Eval(Container.DataItem,
"customer_id")%>' >
</asp:TextBox></td>
<td colspan="2">
<asp:TextBox ID="custNametxt" runat="server"
Width="307px" Text ='<%# DataBinder.Eval(Container.DataItem,
"customer_name")%>' ></asp:TextBox></td>
<td style="width: 129px">
<asp:TextBox ID="adNumbertxt" runat="server"
Text=' said:
</asp:TextBox></td>
</tr><tr>
<td style="width: 161px; height: 14px;">
Publication</td>
<td style="width: 309px; height: 14px;">
Publication Date</td>
<td style="width: 162px; height: 14px;">
Columns</td>
<td style="width: 129px; height: 14px;">
Depth</td>
</tr>
<tr>
<td style="width: 161px; height: 20px">
<asp:TextBox ID="publicationtxt" runat="server"
Text =' said:
</asp:TextBox></td>
<td style="width: 309px; height: 20px">
<asp:TextBox ID="pubdatetxt" runat="server"
Text=' said:
</asp:TextBox></td>
<td style="width: 162px; height: 20px">
<asp:TextBox ID="columnstxt" runat="server"
Text=' said:
</asp:TextBox></td>
<td style="width: 129px; height: 20px">
<asp:TextBox ID="depthtxt" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "depth")%>' ></asp:TextBox></td>
</tr>
<tr>
<td style="width: 161px; height: 13px">
Zone</td>
<td style="width: 309px; height: 13px">
Class/Section</td>
<td style="width: 162px; height: 13px">
Location</td>
<td style="width: 129px; height: 13px">
Lineage</td>
</tr>
<tr>
<td style="width: 161px; height: 26px">
<asp:TextBox ID="zonetxt" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "ad_zone")%>' ></asp:TextBox>
</td>
<td style="width: 309px; height: 26px">
<asp:TextBox ID="class_sectiontxt" runat="server"
Text=' said:
</asp:TextBox>
</td>
<td style="width: 162px; height: 26px">
<asp:TextBox ID="locationtxt" runat="server" Text
=' said:
</asp:TextBox></td>
<td style="width: 129px; height: 26px">
<asp:TextBox ID="lineagetxt" runat="server" Text
=' said:
</asp:TextBox></td>
</tr>
<tr>
<td colspan="3" style="height: 12px">
Ad Text</td>
<td style="width: 129px; height: 12px">
<strong>Net Price</strong></td>
</tr>
<tr>
<td colspan="3" style="height: 26px">
<asp:TextBox ID="ad_texttxt" runat="server"
Width="469px" Text ='<%# DataBinder.Eval(Container.DataItem,
"ad_text")%>' ></asp:TextBox></td>
<td style="width: 129px; height: 26px">
<asp:TextBox ID="netpricetxt" runat="server" Text
=' said:
</asp:TextBox></td>
</tr>
</table><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<table style="width: 319px">
<tr>
<td>
<asp:Button ID="ResetThisAd" runat="server"
CommandArgument="1" CommandName="ResetThisAd" Text="Reset This Ad"
/></td>
<td>
<asp:Button ID="AdjThisAd" runat="server"
CommandArgument="2" CommandName="AdjustThisAd" Text="Adjust This Ad"
/></td>
</tr>
</table>
<br />
<hr />
</ItemTemplate>

</asp:Repeater>

<table style="width: 292px; text-align: left">
<tr>
<asp:button ID="PageClick" OnClick="Page_Click"
runat="server" Visible="false"></asp:button>
<asp:label ID="Info" runat="server"></asp:label>

<td style="width: 67px; height: 20px">
<asp:LinkButton ID="FirstPage" runat="server"
CommandName="FirstPage"
OnCommand="Page_Changed">FirstPage</asp:LinkButton></td>
<td style="width: 33px; height: 20px">
<asp:LinkButton ID="PrevPage" runat="server"
CommandName="PrevPage"
OnCommand="Page_Changed">PrevPage</asp:LinkButton></td>
</asp:Label></td>
<td style="width: 36px; height: 20px">
<asp:LinkButton ID="NextPage" runat="server"
CommandName="NextPage"
OnCommand="Page_Changed">NextPage</asp:LinkButton></td>
<td style="width: 35px; height: 20px">
<asp:LinkButton ID="LastPage" runat="server"
CommandName="LastPage"
OnCommand="Page_Changed">LastPage</asp:LinkButton></td>
</tr>
</table>
<br />
<br />
<table style="width: 205px">
<tr>
<td>
<asp:Button ID="cmdPrev" runat="server" Text="<<
Prev" OnClick="cmdPrev_Click" /></td>
</tr>
</table>
<br />
</div>
</form>
</body>
</html>

--------------------------------------------------------------
Codebehind
--------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SearchAds : System.Web.UI.Page
{
PagedDataSource objAllAds = new PagedDataSource();
DataSet PendingAllDs;
string userid;
ArrayList getAllAds;
Badj.UpdateStatus GetAllPendingAds;
DataSet ds;
int totalRecords;
Badj.UpdateAllPendingAds UpdateAdInfo;
protected void Page_Load(object sender, EventArgs e)
{

userid = Request.ServerVariables["LOGON_USER"].Split('\\')[1];
Session["userid"] = userid;
Session["pageIndex"] = 1;
/*if (IsPostBack)
{
getAllAds = new ArrayList();
UpdateAdInfo = new Badj.UpdateAllPendingAds();
UpdateAdInfo.SalesRep = Convert.ToInt32 ( Session["SalesRep"]);
UpdateAdInfo.FromDate = Convert.ToDateTime ( Session["FromDate"]);
UpdateAdInfo.ToDate = Convert.ToDateTime ( Session["ToDate"]);
UpdateAdInfo.AdNum = Convert.ToInt32(Session["AdNum"]);
getAllAds.Add(UpdateAdInfo);

BindData();
}*/

}

public int CurrentPage
{
get
{
// look for current page in ViewState
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0; // default page index of 0
else
return (int)o;
}

set
{
this.ViewState["_CurrentPage"] = value;
}
}
protected void cmdPrev_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
PendingAllDs = new DataSet();
PendingAllDs.ReadXml("c:\\" + userid + "All.xml");
objAllAds.DataSource = PendingAllDs.Tables["PendingAds"].DefaultView;

objAllAds.AllowPaging = true;

objAllAds.PageSize = 3;

objAllAds.CurrentPageIndex = CurrentPage - 1;

cmdPrev.Enabled = !objAllAds.IsFirstPage;
cmdNext.Enabled = !objAllAds.IsLastPage;

SearchRepeater.DataSource = objAllAds;
Page.DataBind();
}
protected void cmdNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
PendingAllDs = new DataSet();
PendingAllDs.ReadXml("c:\\" + userid + "All.xml");
objAllAds.DataSource = PendingAllDs.Tables["PendingAds"].DefaultView;

objAllAds.AllowPaging = true;

objAllAds.PageSize = 3;

objAllAds.CurrentPageIndex = CurrentPage + 1;

cmdPrev.Enabled = !objAllAds.IsFirstPage;
cmdNext.Enabled = !objAllAds.IsLastPage;

SearchRepeater.DataSource = objAllAds;
Page.DataBind();

}
protected void SearchRepeater_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
RepeaterItem ri = (RepeaterItem)e.Item;
TextBox b;
Button AdjThisAd;
Button ResetThisAd;
//ArrayList AdjThisAd = new ArrayList();

string str;

if (ri.ItemType == ListItemType.Item ||
ri.ItemType==ListItemType.AlternatingItem )
{
AdjThisAd = ri.FindControl("AdjThisAd") as Button;
ResetThisAd = ri.FindControl("ResetThisAd") as Button;

int j = 0;
foreach (RepeaterItem item in SearchRepeater.Items)
{
string[] valCheck = AdjThisAd.ClientID.Split('_');
int itemNumber = Convert.ToInt32(valCheck[1].Remove(0, 3));

string[] valCheck2 = ResetThisAd.ClientID.Split('_');
int itemNumber2 = Convert.ToInt32(valCheck2[1].Remove(0, 3));
string strk = AdjThisAd.CommandArgument;
string str4 = ResetThisAd.CommandArgument;

if (j == itemNumber && e.CommandName == "AdjustThisAd")
{
b = item.FindControl("custIdtxt") as TextBox;
Session["str"] = b.Text;
string url = "Rebate.aspx";
Response.Write("<script>window.open('" + url + "');</script>");
break;
}
if (j == itemNumber2 && e.CommandName =="ResetThisAd")
{
Response.Write("done chitti");
}
j+=1;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
getAllAds = new ArrayList();
UpdateAdInfo = new Badj.UpdateAllPendingAds();
if (Radio1.Checked == true)
{
UpdateAdInfo.SalesRep = Convert.ToInt32(SalesRep.Value.ToString());
UpdateAdInfo.FromDate =
Convert.ToDateTime(FromDate.Value.ToString());
UpdateAdInfo.ToDate = Convert.ToDateTime(ToDate.Value.ToString());

Session["SalesRep"] = Convert.ToInt32(SalesRep.Value.ToString());
Session["FromDate"] = Convert.ToDateTime(FromDate.Value.ToString());
Session["ToDate"] = Convert.ToDateTime(ToDate.Value.ToString());
}
else
{
if (AdNum.Value != "")
{
UpdateAdInfo.AdNum = Convert.ToInt32(AdNum.Value.ToString());
Session["AdNum"] = Convert.ToInt32(AdNum.Value.ToString());
}
}
getAllAds.Add(UpdateAdInfo);

BindData ();

}

public void Page_Changed(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "FirstPage":
PageNumber.Value = "1";
break;
case "PrevPage":
PageNumber.Value = (int.Parse(PageNumber.Value) - 1).ToString();
break;
case "NextPage":
PageNumber.Value = (int.Parse(PageNumber.Value) + 1).ToString();
break;
case "LastPage":
PageNumber.Value = Pages.Value;
break;
}
PendingAllDs = GetAllPendingAds.GetAllPendingAds(getAllAds);
//PendingAllDs.ReadXml("c:\\" + userid + "All.xml");
objAllAds.DataSource = PendingAllDs.Tables["PendingAds"].DefaultView;
totalRecords = PendingAllDs.Tables["PendingAds"].Rows.Count;
Pages.Value = Math.Ceiling((double)totalRecords / 18).ToString();


int pageNumber = int.Parse(PageNumber.Value);
int totalPages = int.Parse(Pages.Value);

PagesDisplay.Text = "";
for (int i = 1; i <= totalPages; i++)
{
if (pageNumber != i)
PagesDisplay.Text += "<a href=\"javascript:ChangePage(" + i +
")\">" + i + "</a>&nbsp;&nbsp;";
else
PagesDisplay.Text += "[" + i + "]&nbsp;&nbsp;";
}

// enable/disable the links to navigate through the pages
FirstPage.Enabled = (pageNumber != 1);
PrevPage.Enabled = (pageNumber != 1);
NextPage.Enabled = (pageNumber != totalPages);
LastPage.Enabled = (pageNumber != totalPages);

}
public void BindData()
{
PendingAllDs = new DataSet();
GetAllPendingAds = new Badj.UpdateStatus(userid);
PendingAllDs = GetAllPendingAds.GetAllPendingAds(getAllAds);
//PendingAllDs.ReadXml("c:\\" + userid + "All.xml");
objAllAds.DataSource =
PendingAllDs.Tables["PendingAds"].DefaultView;
totalRecords = PendingAllDs.Tables["PendingAds"].Rows.Count;
Pages.Value = Math.Ceiling((double)totalRecords / 18).ToString();

objAllAds.PageSize = 18;

SearchRepeater.DataSource = objAllAds;
SearchRepeater.DataBind();
int pageNumber = int.Parse(PageNumber.Value);
int totalPages = int.Parse(Pages.Value);

PagesDisplay.Text = "";
for (int i = 1; i <= totalPages; i++)
{
if (pageNumber != i)
PagesDisplay.Text += "<a href=\"javascript:ChangePage(" + i +
")\">" + i + "</a>&nbsp;&nbsp;";
else
PagesDisplay.Text += "[" + i + "]&nbsp;&nbsp;";
}

// enable/disable the links to navigate through the pages
FirstPage.Enabled = (pageNumber != 1);
PrevPage.Enabled = (pageNumber != 1);
NextPage.Enabled = (pageNumber != totalPages);
LastPage.Enabled = (pageNumber != totalPages);
//objAllAds.AllowPaging = true;

//objAllAds.PageSize = 3;
//if(CurrentPage !=0)
//objAllAds.CurrentPageIndex = CurrentPage - 1;

//cmdPrev.Enabled = !objAllAds.IsFirstPage;
//cmdNext.Enabled = !objAllAds.IsLastPage;

}
public void Page_Click(object sender, System.EventArgs e)
{
PendingAllDs.ReadXml("c:\\" + userid + "All.xml");
objAllAds.DataSource = PendingAllDs.Tables["PendingAds"].DefaultView;
totalRecords = PendingAllDs.Tables["PendingAds"].Rows.Count;
Pages.Value = Math.Ceiling((double)totalRecords / 18).ToString();


int pageNumber = int.Parse(PageNumber.Value);
int totalPages = int.Parse(Pages.Value);

PagesDisplay.Text = "";
for (int i = 1; i <= totalPages; i++)
{
if (pageNumber != i)
PagesDisplay.Text += "<a href=\"javascript:ChangePage(" + i +
")\">" + i + "</a>&nbsp;&nbsp;";
else
PagesDisplay.Text += "[" + i + "]&nbsp;&nbsp;";
}

// enable/disable the links to navigate through the pages
FirstPage.Enabled = (pageNumber != 1);
PrevPage.Enabled = (pageNumber != 1);
NextPage.Enabled = (pageNumber != totalPages);
LastPage.Enabled = (pageNumber != totalPages);
}
protected void SearchRepeater_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
RepeaterItem ri = (RepeaterItem)e.Item;
TextBox b;
Button AdjThisAd;
Button ResetThisAd;
//ArrayList AdjThisAd = new ArrayList();
//BindData();
string str;

if (ri.ItemType == ListItemType.Item || ri.ItemType ==
ListItemType.AlternatingItem)
{
AdjThisAd = ri.FindControl("AdjThisAd") as Button;
ResetThisAd = ri.FindControl("ResetThisAd") as Button;

int j = 0;
foreach (RepeaterItem item in SearchRepeater.Items)
{
string[] valCheck = AdjThisAd.ClientID.Split('_');
int itemNumber = Convert.ToInt32(valCheck[1].Remove(0, 3));

string[] valCheck2 = ResetThisAd.ClientID.Split('_');
int itemNumber2 = Convert.ToInt32(valCheck2[1].Remove(0, 3));

if
(PendingAllDs.Tables["PendingAds"].Rows[itemNumber]["ad_status_id"].ToString()
== "0")
{
ResetThisAd.Enabled = false;
}
}
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------


Thanks for you time,

Regards,
Ratnakar Pedagani
 
J

john_teague

There is no built in paging for a repeater. You would need to do it
yourself.

One possibility is to put the table structure you have in the
ItemTemplate into a User Control and then place the user control inside
a DataGrid or DataList.

User Controls are not difficult. Usually when I need to nest them in a
List, I use an initialize method that I call in the Grid's
ItemDataBound event and pass in the DataItem for that row to set any
properties for the control. There are probably better ways, but
haven't found it yet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top