473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How to toggle visibilty of button inside repeater control

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
1 2900
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Keith Harris | last post by:
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. ...
2
by: Stephen Miller | last post by:
I am using the OnItemDataBound event of Repeater control to nest a DataGrid within the Repeater. When I attempt to bind to the DataGrid using the DataSource method I get the error message "Object...
9
by: Ric | last post by:
im new to asp.net. please help if u can. is it possible to refer to a control(ie lable, placeholder, textbox) that is inside a repeater object from a code behind file? when i place the control...
3
by: Leigh Webber | last post by:
I have an HTMLAnchor control on my aspx page. When it's not inside a repeater, it works fine. When I put it inside a repeater control, the handler never gets fired. I have a handler for the...
1
by: Lukas Kurka | last post by:
Hi, I have a asp:repeater and inside it I have asp:button control. How can I add javascript onclick function (and also another) to the button. Thank you for your help
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
1
by: Annyka | last post by:
I've got a button inside a Repeater control Item Template I'm doing a SQL insert using a SqlDataSource Control based on the RepeaterName_ItemCommand event, since the button in the template isn't...
4
by: Nathan Sokalski | last post by:
I have a Button that uses the PostBackUrl property which is inside a Repeater template. When I get to the Page that is being posted to (the one specified in PostBackUrl), I am having trouble...
1
by: semomaniz | last post by:
I have a button inside a repeater which is supposed to open a popup when clicked. But when i click on the button my modalpopup does not open. The strange thing is on the code provided below if i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.