ForEach loop to get control values from PanelBar

4 posts, 0 answers
  1. Jeff
    Jeff avatar
    38 posts
    Member since:
    Jan 2008

    Posted 14 Jan 2009 Link to this post

    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
  2. Jeff
    Jeff avatar
    38 posts
    Member since:
    Jan 2008

    Posted 15 Jan 2009 Link to this post

    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

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 19 Jan 2009 Link to this post

    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.
  5. Jeff
    Jeff avatar
    38 posts
    Member since:
    Jan 2008

    Posted 19 Jan 2009 Link to this post

    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.
Back to Top