Solved

Access the ID of the repeater being rendered

Posted on 2007-04-10
6
233 Views
Last Modified: 2013-12-17
I have a page with three repeater controls on it.  Each one has a label that displays an aggregate number in a label within the footer column.  I've written a function determines which repeater is being rendered and should populate the label with the appropriate value but it's bombing out.  I can't figure out what to put in the switch statement (I tried to use sender.id), so that the right label will be chosen.  Should be an easy one:

    protected void Repeater_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if ((mTotalRecords != 0) && (e.Item.ItemType == ListItemType.Footer))
        {
            string sName = sender;

            Label lblTotal;

            switch (sName.ToString())
            {
                 
                case "rptBB":
                    lblTotal = (Label)e.Item.FindControl("lblBBTotal");
                    break;
                case "rptPhone":
                    lblTotal = (Label)e.Item.FindControl("lblPhoneTotal");
                    break;
                case "rptWWAN":
                    lblTotal = (Label)e.Item.FindControl("lblWWANTotal");
                    break;
            }

            lblTotal.Text = "Total: " + mTotalRecords;
        }
     }
0
Comment
Question by:stretch73
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
dmagliola earned 350 total points
ID: 18887595
The sender parameter is going to be of type Repeater, so you should change the line that says:

string sName = sender;

to something like:

Repeater repCtl = (Repeater) sender;

Then, in the switch statement, you query it's ID:

switch (sName.ID)

which will let you decide which one of the 3 was the sender.
0
 

Author Comment

by:stretch73
ID: 18890087
dmagliola

That got it about halfway there.  Here is the updated code:

    protected void Repeater_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if ((mTotalRecords != 0) && (e.Item.ItemType == ListItemType.Footer))
        {
            Label lblTotal;
            Repeater repCtl = (Repeater)sender;
            string sName = repCtl.ID.ToString();

            switch (sName)
            {
                 
                case "rptBB":
                    lblTotal = (Label)e.Item.FindControl("lblBBTotal");
                    break;
                case "rptPhone":
                    lblTotal = (Label)e.Item.FindControl("lblPhoneTotal");
                    break;
                case "rptWWAN":
                    lblTotal = (Label)e.Item.FindControl("lblWWANTotal");
                    break;
            }

            lblTotal.Text = "Total: " + mTotalRecords;
        }
     }

It's erroring out here:
"Use of unassigned local variable 'lblTotal'"
Line 122:            lblTotal.Text = "Total: " + mTotalRecords;

I put a breakpoint in the procedure and the id is showing up in each iteration, not sure why it's ending up unassigned.
0
 

Author Comment

by:stretch73
ID: 18890323
Forgot to add:

Label lblTotal = new Label();


Thanks dmag, that was a stumper.


0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Expert Comment

by:dmagliola
ID: 18890872
Actually, declaring the label as a New Label will get you past the error, but is it working?
I mean, you're creating a new label, instead of modifying the existing one (the one you should be finding through FindControl)

It seems to me that FindControl is returning null (because it can't find the control) or the ID of your sender is not in one of the 3 cases you are expecting, and so it doesn't get assigned.

These are the things I can think about, but I may be missing something.
0
 

Author Comment

by:stretch73
ID: 18891273
It's working.  The sender was always one of the three cases so I guess I just needed to initialize the label control.  Thanks again.
0
 
LVL 3

Expert Comment

by:dmagliola
ID: 18891660
Excellent!
Could you mark me as the answer for this one?
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET data base connection 35 95
PowerShell: Adding ToGB to a script 4 74
VS C# how to design libary of functions 7 33
How does this modal work? 3 33
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question