473,796 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 NullReferenceEx ception 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.Data Item,"GroupName ") %></td>
<td><%# DataBinder.Eval (Container.Data Item,"NumUsers" ) %></td>
</tr>
</ItemTemplate>
<FooterTemplate >
<tr>
<td>
<asp:Button id="buttonNext " runat="server" Text="Next"
OnClick="button Next_Click" Visible="True"> </asp:Button>
</td>
</tr>
</table>
</FooterTemplate>

=============== =============== ============
WebForm.aspx.cs
=============== =============== ============
....
protected System.Web.UI.W ebControls.Repe ater Repeater1;
protected System.Web.UI.W ebControls.Butt on buttonNext;
....
private void Page_Load(objec t sender, System.EventArg s e)
{
this.Repeater1. DataSource=dsDa taSet;
this.Repeater1. DataBind();

object
sum=dsDataSet.T ables[0].Compute("SUM(N umUsers)","NumU sers=NumUsers") ;

buttonNext.Visi ble=( (int)sum > 0 );
}
Nov 18 '05 #1
1 2935
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("bu ttonNext")
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.ispostba ck){
DataSet ds = GetData();
sum = //figure out sum;
repater.DataSou rce = ds;
repeater.OnItem DataBound += ...
repeater.DataBi nd();
}
}

and then use the sum to toggle visibility.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Keith Harris" <Ke*********@di scussions.micro soft.com> wrote in message
news:F8******** *************** ***********@mic rosoft.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 NullReferenceEx ception 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.Data Item,"GroupName ") %></td>
<td><%# DataBinder.Eval (Container.Data Item,"NumUsers" ) %></td>
</tr>
</ItemTemplate>
<FooterTemplate >
<tr>
<td>
<asp:Button id="buttonNext " runat="server" Text="Next"
OnClick="button Next_Click" Visible="True"> </asp:Button>
</td>
</tr>
</table>
</FooterTemplate>

=============== =============== ============
WebForm.aspx.cs
=============== =============== ============
...
protected System.Web.UI.W ebControls.Repe ater Repeater1;
protected System.Web.UI.W ebControls.Butt on buttonNext;
...
private void Page_Load(objec t sender, System.EventArg s e)
{
this.Repeater1. DataSource=dsDa taSet;
this.Repeater1. DataBind();

object
sum=dsDataSet.T ables[0].Compute("SUM(N umUsers)","NumU sers=NumUsers") ;

buttonNext.Visi ble=( (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
1741
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. 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".
2
3304
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 reference not set to an instance of an object". This error message commonly occurs when a server control is incorrecly declared, so naturally I have double checked this. To test this, I moved the aspx code for the DataGrid ('myNestedDataGrid')...
9
10155
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 object outside of the repeater, i can refer to it from the code behind file. when i place the control object inside the repeater, i get a 'need to instanciate the control object' error. if i declare the control object inside the control behind file,...
3
5682
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 htmlAnchor's ServerClick event (works when not inside a repeater), and a handler for the repeater's ItemCommand event. My html and vb source code is shown below, but what I think I need is a simple working example in vb.net. Can anyone help?...
1
3350
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
3026
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 way I'm going about doing this might be a little off. I'd appreciate some help. Below is the code I have thus far but I'm not sure how to reference the user control within the foreach loop. <asp:Panel ID="pnlRosterProfile" runat="Server" />
1
2392
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 available on the code behind file for a click event. After the SQL command has run, I'd like to post back to another page. When I set the PostBackURL property, it posts back before the insert is performed. Can I either:
4
4129
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 getting access to the Button that was clicked, because I cannot use the PreviousPage.FindControl() method since I do not know the id of the Button. What can I do to get access to a Button that was inside a Repeater? Thanks. Nathan Sokalski...
1
8063
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 replace the button with linkbutton every thing works fine and I have my popup dispalying. Is there a reason for the buttons to not function inside the repeater? <asp:Repeater ID="myrep" runat="server" OnItemCommand="myrep_ItemCommand">...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.