stretch73
asked on
Access the ID of the repeater being rendered
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(objec t 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( "lblBBTota l");
break;
case "rptPhone":
lblTotal = (Label)e.Item.FindControl( "lblPhoneT otal");
break;
case "rptWWAN":
lblTotal = (Label)e.Item.FindControl( "lblWWANTo tal");
break;
}
lblTotal.Text = "Total: " + mTotalRecords;
}
}
protected void Repeater_ItemCreated(objec
{
if ((mTotalRecords != 0) && (e.Item.ItemType == ListItemType.Footer))
{
string sName = sender;
Label lblTotal;
switch (sName.ToString())
{
case "rptBB":
lblTotal = (Label)e.Item.FindControl(
break;
case "rptPhone":
lblTotal = (Label)e.Item.FindControl(
break;
case "rptWWAN":
lblTotal = (Label)e.Item.FindControl(
break;
}
lblTotal.Text = "Total: " + mTotalRecords;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Forgot to add:
Label lblTotal = new Label();
Thanks dmag, that was a stumper.
Label lblTotal = new Label();
Thanks dmag, that was a stumper.
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.
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.
ASKER
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.
Excellent!
Could you mark me as the answer for this one?
Could you mark me as the answer for this one?
ASKER
That got it about halfway there. Here is the updated code:
protected void Repeater_ItemCreated(objec
{
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(
break;
case "rptPhone":
lblTotal = (Label)e.Item.FindControl(
break;
case "rptWWAN":
lblTotal = (Label)e.Item.FindControl(
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.