This is a migrated thread and some comments may be shown as answers.

ForEach loop to get control values from PanelBar

3 Answers 372 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 15 Jan 2009, 12:39 AM
We've got a PanelBar that is built dynamically from the code behind with values pulled from a database.  We're using the itemdatabound even to add a textbox control to each entry at the third level. This part is working great due to some excellent input from the Telerik support staff.

The idea now is for the user to enter scores into each textbox which are then saved when they click a save button. I'm having trouble figuring out how to access the dynamically built controls and then how to put it into a foreach loop so that we can save those values back to the database.

Trying to adapt the example from the Telerik help doesn't seem like it would work since the control is dynamically bult:
TextBox Score = (TextBox)RadScoringPanel.FindItemByValue("?").FindControl("?");

So for instance the panelbar looks something like this:
- Math Scores
--December
---John [text box]
---Mary [text box]
--January
---John [text box]
---Mary [text box]
-English Scores
--etc.

< - Save - >

So when the scores are entered and then the save button is clicked I need to retrieve them and then put them into a:
"foreach (e.item..." type loop but this is where we're stuck. Can't figure out how to retrieve the value and also how to identify each panel line item for the foreach loop statement. Just to make it a bit more complicated I need to also pull the ID/Value for each level of the hierarchy for the item so that we can save it appropriately.

For instance for John's December Math Score I would need the ID for "Math Scores" (level 1), the ID for "December" (level 2) and the ID for John along with the Value in the textbox so that we can associate the score correctly.

Any direction/pointers would be greatly appreciated.

thanks

3 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 15 Jan 2009, 11:43 PM
I think I'm getting close but am now hitting the dreaded "object reference not set to an instance of an object" from the findcontrol.

So at first I'm using the ItemDataBound event to set add the textbox control to the panelbar and that's working well:

protected void RadScoringPanel_ItemDataBound(object sender, Telerik.Web.UI.RadPanelBarEventArgs e) 
   {    
    if (e.Item.Level == 2) 
    {     
     TextBox tb = new TextBox(); 
     tb.Text = "0";
     tb.ID=e.Item.Value;
     tb.Style["text-align"] = "center";
     tb.Style["border-bottom"]="solid 1px #cccccc"; 
     tb.Width = Unit.Pixel(70); 
     e.Item.Controls.Add(tb);
etc.

but then later when I try to find the control to get the value I'm getting the object reference error:

foreach (RadPanelItem panelitem in RadScoringPanel.GetAllItems())
        {
           if (panelitem.Level == 2)  
            { 
                string sItemValue = (panelitem.Value).ToString(); 
                TextBox txtScore = (TextBox)panelitem.FindControl(sItemValue);
            etc.

Any thoughts on why the result of this is null which causes the object reference error? If I retrieve the variable for the panelitem.Value it is correct. The source code shows the "id" for each of the controls as id="ctl00_ctl00_RemotePrimary_RemoteMain_RadScoringPanel_i0_i0_i2_5" with the "5" at the end being the actual value from the panelitem. Is the additional code before the "5" causing the issue?

Help!! Thanks

0
Veselin Vasilev
Telerik team
answered on 19 Jan 2009, 10:55 AM
Hello Jeff,

You need to create the TextBox control in the ItemCreated event. In ItemDataBound event you should only initialize its value.

I have prepared a sample project - please download it and give it a try.
Note: You need to expand the second panelbar item (Sports).


Regards,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jeff
Top achievements
Rank 1
answered on 19 Jan 2009, 04:58 PM
Thank you Veselin! You are a miracle worker. I spent almost a week trying to figure out a way to make that work and was just about to give up and go in a different direction. Your solution worked perfectly.
Tags
PanelBar
Asked by
Jeff
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Share this question
or