How can I get to a programmatically filled Panel in a GridView RowUpdating event?

Last post 03-24-2008 5:20 PM by edmicman. 9 replies.

Sort Posts:

  • How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-19-2008, 1:22 PM
    • Member
      258 point Member
    • edmicman
    • Member since 08-04-2005, 5:11 PM
    • Posts 110

     I'm in ASP.NET 2.0, C#, VS2008.  I have a GridView with a TemplateField, with this in the EditItemTemplate:

     

    <EditItemTemplate>
                            <asp:Label ID="lblEditCoverageHeader" runat="server" Text=''>
                            </asp:Label><asp:Panel
                                ID="panelEditCovgReqs" runat="server">
                            </asp:Panel>
                        </EditItemTemplate>

      In the GridView RowDataBound event I'm programmatically adding controls (TextBoxes, CheckBoxes, and DropDownLists) to the Panel.  This all shows up fine when I edit the gridrow.  What I need, is how can I then access the controls inside the panel when RowUpdating fires?  I thought I could FindControl on the Panel, then loop through the Panel.Controls collection, check if each control is one of those types, and then do appropriate processing for each.  I tried this in the RowUpdating:

     

    GridView gvTemp = (GridView)sender;
            GridViewRow row = gvTemp.Rows[e.RowIndex];
            foreach (TableCell cell in row.Cells)
            {
                foreach (Control c in cell.Controls)
                {
                    if (c.GetType() == typeof(Panel))
                    {
                        Panel panelEditCovgReqs = (Panel)c.FindControl("panelEditCovgReqs");
                    }
                }
            }
      and it does seem to find the Panel control....but it doesn't show any controls in that Panel.  In fact, I believe it only shows a "WebLiteral" control??  I thought I could just do a FindControl on the GridViewRow anyway, but that didn't seem to work, either.  I'm sure this has got to be simpler than what I'm making it.....any help?  Thanks!!
  • Re: How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-19-2008, 3:01 PM
    • Participant
      906 point Participant
    • andudarev
    • Member since 02-26-2008, 7:04 PM
    • Canada, Toronto
    • Posts 179

    It happens because RowDataBound is not fired when you update GridView.

    Probably you have to create CustomControl and then add this control instead of Panel.

    This control should implement some ViewState logic.

     

    Andrey Dudarev
    ...для нас невозможного мало...
  • Re: How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-19-2008, 3:36 PM
    • Member
      258 point Member
    • edmicman
    • Member since 08-04-2005, 5:11 PM
    • Posts 110
    Ack, that's frustrating.  The values from the controls created aren't contained in the viewstate/postback when I hit the update?  Thanks for the tip, I'll check it out.
  • Re: How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-19-2008, 3:54 PM
    • Contributor
      5,201 point Contributor
    • aamador
    • Member since 02-11-2008, 10:49 PM
    • Posts 1,107

    RowCommand or RowUpdating is a better place because I don't believe the controls are in scope untill the grid is in edit mode.  The same thing happens when in edit mode the itemTemplate are not accessible. move panel or copy over ItemTemplate and see if you can then see it. 

    I am not anti social, am just not user friendly
  • Re: How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-19-2008, 5:00 PM
    • Member
      258 point Member
    • edmicman
    • Member since 08-04-2005, 5:11 PM
    • Posts 110

     Hmmmm.....maybe I'm going about this all wrong?  This is what I want to do:

    I have a GridView, which displays a list of items.  This is working fine.  Each of the items in the GridView may or may not have a set of associated properties that are stored in my database.  Give each GridView item number, I query the database, assemble the collection of properties, and build a Panel containing controls for these properties.  That's probably not very clear, is it?  :-)

    When I edit a row in the GridView, I want the Panel to display for the selected row, containing the appropriate controls.  Maybe I should build this panel in RowEditing rather than RowDataBound?  I'm not actually binding any data - I just want the GridView for it's layout and display purposes.  I'm going to handle the actual editing/updating of data manually. 

  • Re: How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-19-2008, 5:40 PM
    • Participant
      906 point Participant
    • andudarev
    • Member since 02-26-2008, 7:04 PM
    • Canada, Toronto
    • Posts 179

    Why don't just put all controls directly to GridView item template and then just swich their Visible property?

    Andrey Dudarev
    ...для нас невозможного мало...
  • Re: How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-19-2008, 5:53 PM
    • Member
      258 point Member
    • edmicman
    • Member since 08-04-2005, 5:11 PM
    • Posts 110

    andudarev:

    Why don't just put all controls directly to GridView item template and then just swich their Visible property?

     

    Hmmmm, maybe I could try that.  I was only using the Panel just so I could logically group things.  I think I'd still need to dynamically add controls to the EditItemTemplate since I don't know how many textboxes, checkboxes, or dropdownlists may apply to each item in the gridview until it's databound.  

    I guess now that I'm thinking it over more, maybe I'm asking the wrong questions?  Specifically, how can I add dynamic non-databound controls to an EditItemTemplate, and then access those values on the PostBack/RowUpdate?  Is it even possible?

  • Re: How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-19-2008, 6:18 PM
    Answer
    • Participant
      906 point Participant
    • andudarev
    • Member since 02-26-2008, 7:04 PM
    • Canada, Toronto
    • Posts 179

    In this case, as I suggested above, you have to implement the CustomControl, which keeps somehow its state (ViewState, ControlState, cache).

    Might be another way is to keep the IDs of elements in your Panel between PostBacks and then just read their values directly from Page.Form["itemID"].

    Andrey Dudarev
    ...для нас невозможного мало...
  • Re: How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-19-2008, 9:46 PM
    • Member
      100 point Member
    • Siby2000
    • Member since 03-03-2008, 12:07 AM
    • Posts 73

    I am also having problem with gridview update command.  Please post the code when you are done with.

    thanks

  • Re: How can I get to a programmatically filled Panel in a GridView RowUpdating event?

    03-24-2008, 5:20 PM
    • Member
      258 point Member
    • edmicman
    • Member since 08-04-2005, 5:11 PM
    • Posts 110

    andudarev:

    Might be another way is to keep the IDs of elements in your Panel between PostBacks and then just read their values directly from Page.Form["itemID"].

     

    Ugh, this still isn't working.  I tried the Form collection and it worked - almost!  I realized that I've got checkboxes, too....and ran into the same problem I've had in classic ASP where the checkbox value doesn't come across the Forms Request if it's not checked.  :-/  I can't just check whether the checkbox is there or not because everything is dynamic and I don't know necessarily if there is a checkbox or not!

    I'm not fully following the ViewState option.  Some articles say that if I create my dynamic controls in the OnInit of the Page, the viewstate will persist - but I can't create this before the page loads...my dynamic controls are part of the GridView which is based on a querystring value.  I saw that the order that I create, set values, and add the dynamic controls to their parents would matter.  I tried that (creating new control, adding it to its parent, and then setting properties), but that didn't work either.

    It sounds like maybe the data from the dynamic controls is somewhere in the viewstate already (as part of the panel I'm adding them to??), and I'm supposed to recreate those controls on postback and if they have the same ID they will retain the values.  But I'm not recreating the controls on postback.  I'm creating the controls dynamically in the EditItemTemplate of the GridView, and only want the data from the controls so I can do the database update after the user clicks "Update" on the GridView.  I only need the data at this point.  Additionally, even if I did write the control value to the ViewState, when do I do it?  I don't want to write the old values, I want whatever the new values would be.  It's like I'd need to add them to the ViewState after a user clicks on or changes the value, but before it submits and posts back?

    Hmmmmm.....I'm back wondering if this is the best way to do what I want to do, or if there's some other way to handle it.  How else might I display a list of items, and for each of those items present a dynamic set of property controls (unknown beforehand), and handle saving and updating these?  I can post some more code, too, if that might help.  Thanks for any ideas!

Page 1 of 1 (10 items)