How to toggle visibilty of button inside repeater control

G

Guest

Hi,

I have a Repeater control which is bound to a dataset. In the footer of the
repeater control, I have a Button whose visibility I want to vary according
to the sum of a column being > 0.

I have tried to set the button's visibility in Page_Load (after data
binding), but I get a NullReferenceException stating that "Object reference
not set to an instance of an object".

I'm sure this has to do with when my repeater control and it's contained
controls are being rendered, I've tried putting the logic in several places
but I can't figure it out. Below is the code; thank you for any help.

-Keith

==========================================
WebForm.aspx
==========================================
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>Name</td>
<td>Num Users</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"GroupName") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"NumUsers") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>
<asp:Button id="buttonNext" runat="server" Text="Next"
OnClick="buttonNext_Click" Visible="True"></asp:Button>
</td>
</tr>
</table>
</FooterTemplate>

==========================================
WebForm.aspx.cs
==========================================
....
protected System.Web.UI.WebControls.Repeater Repeater1;
protected System.Web.UI.WebControls.Button buttonNext;
....
private void Page_Load(object sender, System.EventArgs e)
{
this.Repeater1.DataSource=dsDataSet;
this.Repeater1.DataBind();

object
sum=dsDataSet.Tables[0].Compute("SUM(NumUsers)","NumUsers=NumUsers");

buttonNext.Visible=( (int)sum > 0 );
}
 
K

Karl Seguin

Keith,
You'll find your answer in my asp.net databinding tutorial:
http://openmymind.net/databinding/index.html#7.2

Basically, you want to hook into the OnItemDataBound event, and check for
e.Item.ItemType == ItemType.Footer

then you can do a
Button btn = (Button)e.Item.FindControl("buttonNext")
and toggle the visible there via btn.Visible = true/false;

Specifically with respect to doing it via the sum of a column being zero,
you'll need to figure that out when you get your dataset and store it in a
class field variable

private int sum = 0;
void page_load{
if (!page.ispostback){
DataSet ds = GetData();
sum = //figure out sum;
repater.DataSource = ds;
repeater.OnItemDataBound += ...
repeater.DataBind();
}
}

and then use the sum to toggle visibility.

Karl
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top