Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to toggle visibilty of button inside repeater control

Reply
Thread Tools

How to toggle visibilty of button inside repeater control

 
 
=?Utf-8?B?S2VpdGggSGFycmlz?=
Guest
Posts: n/a
 
      11-05-2004
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 );
}
 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      11-05-2004
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" <> wrote in message
news:F857D75F-1571-44DC-87C1-...
> 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 );
> }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Toggle GridView inside Gridview David C ASP .Net 3 11-27-2010 05:35 AM
Increasing Tooltip visibilty geronimi ASP .Net 2 01-11-2007 07:41 PM
Trying to control the visibilty property of panel in timer event ravidhixith@gmail.com ASP .Net 2 03-10-2006 05:04 AM
Repeater inside a repeater....how? voidfill3d@yahoo.com ASP .Net 1 08-10-2005 01:58 PM
Setting the visibilty of server controls Anita C ASP .Net Web Controls 2 02-04-2004 02:54 PM



Advertisments