By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

How to toggle visibilty of button inside repeater control

P: n/a
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 );
}
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Keith Harris" <Ke*********@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
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 );
}

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.