Repeater control

S

SAL

I have a web form that use the Repeater control on it. Here is what the code
snippet looks like:

<table class="collector" style="width: 870px">
<asp:repeater id="repeaterItems" runat="server">
<itemtemplate>
<tr>
<td class="logoColumn" colspan="1" style="width:
60px">
<img src="Images/user_headset.png" alt=""/></td>
<td class="tableHead" colspan="9" style="width:
820px">
Customer Group: <%=
DataHelper.SafeGetValue(this.CollectorDataSet, 0, 0, "Customer_Group")%>
</td>
</tr>
</itemtemplate>
</asp:repeater>

<tr>
<td style="width: 60px; height: 50px" class="bodyStyle2">
</td>
<td class="bodyStyle2" colspan="1" style="width: 726px;
height: 50px">
<asp:Button ID="cmdPrev" runat="server"
OnClick="cmdPrev_Click" Text=" << Previous " BackColor="White"
BorderColor="Blue" BorderStyle="Solid" BorderWidth="1px" ForeColor="Black"
Width="91px" /></td>
<td class="statSubtitle1">
<asp:label id="lblCurrentPage" runat="server"
CssClass="statSubtitle1" Width="60px"></asp:label>
</td>
<td class="bodyStyle2" style="width: 98px; height: 50px">
<asp:Button ID="cmdNext" runat="server"
OnClick="cmdNext_Click" Text=" Next >> " BackColor="White"
BorderColor="Blue" BorderStyle="Solid" BorderWidth="1px" ForeColor="Black"
Width="91px" />
</td>
<td class="bodyStyle2" style="width: 56px; height: 50px">
</td>
</tr>
</table>


All examples I’ve seen use <%# DataBinder.Eval(Container.DataItem,
"Customer_Group") %> to bind data to the Repeater, but I am using my own
custom C# class that binds the repeater which works fine. However, when I
click the Next button on my form it does not move to the next record. It
keeps repeating the same record over and over even though my repeater control
is being binded with the correct record count. I'm not sure if the
DataBinder and Container go hand in hand with the repeator and thats why my
the Next/Previous buttons don't move to the next record. Here’s how I bind
the repeater:

private void ItemsGet()
{

// Populate the repeater control with the Items DataSet
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = MyDataSet.Tables[0].DefaultView;
//Items.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 1;

objPds.CurrentPageIndex = CurrentPage;

lblCurrentPage.Text = (CurrentPage + 1).ToString() + " of "
+ objPds.PageCount.ToString();

// Disable Prev or Next buttons if necessary
cmdPrev.Enabled = !objPds.IsFirstPage;
cmdNext.Enabled = !objPds.IsLastPage;

repeaterItems.DataSource = objPds;
repeaterItems.DataBind();
}


Here is the code for the Next Previous buttons

protected void cmdPrev_Click(object sender, EventArgs e)
{
// Set viewstate variable to the previous page
CurrentPage -= 1;

// Reload control
ItemsGet();


}

protected void cmdNext_Click(object sender, EventArgs e)
{
// Set viewstate variable to the next page
CurrentPage += 1;

// Reload control
ItemsGet();
}

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

set
{
this.ViewState["_CurrentPage"] = value;
}
}

Any suggestions would be helpful since I’ve programmed more in C# than
ASP.net.

Thanks
 
N

Nathan Sokalski

I have never written or seen anybody else write there own code to use in
place of DataBinder.Eval, but I would not recommend it anyway. Although one
thing I did notice is that you us <%= %> instead of <%# %>, which could be
having an effect on the repeating of the same data. Something else that I
noticed was that the following code:

<%=DataHelper.SafeGetValue(this.CollectorDataSet, 0, 0, "Customer_Group")%>

does not have anything that specifies which record to use, and since it does
not use <%# %> which have the specific purpose of being used for
databinding, I am not sure how your code would know what record to use. I
would recommend doing a debug session to see exactly what is happening at
each stage in the databinding. This is the best advice I can give, since I
have never seen anybody try to write a replacement for DataBinder.Eval and
although I know how to use databinding, I do not know a ton about the
internal coding of classes such as DataBinder.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

SAL said:
I have a web form that use the Repeater control on it. Here is what the
code
snippet looks like:

<table class="collector" style="width: 870px">
<asp:repeater id="repeaterItems" runat="server">
<itemtemplate>
<tr>
<td class="logoColumn" colspan="1" style="width:
60px">
<img src="Images/user_headset.png"
alt=""/></td>
<td class="tableHead" colspan="9" style="width:
820px">
Customer Group: <%=
DataHelper.SafeGetValue(this.CollectorDataSet, 0, 0, "Customer_Group")%>
</td>
</tr>
</itemtemplate>
</asp:repeater>

<tr>
<td style="width: 60px; height: 50px" class="bodyStyle2">
</td>
<td class="bodyStyle2" colspan="1" style="width: 726px;
height: 50px">
<asp:Button ID="cmdPrev" runat="server"
OnClick="cmdPrev_Click" Text=" << Previous " BackColor="White"
BorderColor="Blue" BorderStyle="Solid" BorderWidth="1px" ForeColor="Black"
Width="91px" /></td>
<td class="statSubtitle1">
<asp:label id="lblCurrentPage" runat="server"
CssClass="statSubtitle1" Width="60px"></asp:label>
</td>
<td class="bodyStyle2" style="width: 98px; height: 50px">
<asp:Button ID="cmdNext" runat="server"
OnClick="cmdNext_Click" Text=" Next >> " BackColor="White"
BorderColor="Blue" BorderStyle="Solid" BorderWidth="1px" ForeColor="Black"
Width="91px" />
</td>
<td class="bodyStyle2" style="width: 56px; height: 50px">
</td>
</tr>
</table>


All examples I've seen use <%# DataBinder.Eval(Container.DataItem,
"Customer_Group") %> to bind data to the Repeater, but I am using my own
custom C# class that binds the repeater which works fine. However, when I
click the Next button on my form it does not move to the next record. It
keeps repeating the same record over and over even though my repeater
control
is being binded with the correct record count. I'm not sure if the
DataBinder and Container go hand in hand with the repeator and thats why
my
the Next/Previous buttons don't move to the next record. Here's how I
bind
the repeater:

private void ItemsGet()
{

// Populate the repeater control with the Items DataSet
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = MyDataSet.Tables[0].DefaultView;
//Items.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 1;

objPds.CurrentPageIndex = CurrentPage;

lblCurrentPage.Text = (CurrentPage + 1).ToString() + " of "
+ objPds.PageCount.ToString();

// Disable Prev or Next buttons if necessary
cmdPrev.Enabled = !objPds.IsFirstPage;
cmdNext.Enabled = !objPds.IsLastPage;

repeaterItems.DataSource = objPds;
repeaterItems.DataBind();
}


Here is the code for the Next Previous buttons

protected void cmdPrev_Click(object sender, EventArgs e)
{
// Set viewstate variable to the previous page
CurrentPage -= 1;

// Reload control
ItemsGet();


}

protected void cmdNext_Click(object sender, EventArgs e)
{
// Set viewstate variable to the next page
CurrentPage += 1;

// Reload control
ItemsGet();
}

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

set
{
this.ViewState["_CurrentPage"] = value;
}
}

Any suggestions would be helpful since I've programmed more in C# than
ASP.net.

Thanks
 

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

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top