#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    16
    Rep Power
    0

    Dynamic controls and asp:button postback


    I'm having an issue saving page content to my database. On a page, I have a bunch of dynamically added controls and a save button at the top of the page. I am using client-side javascript to manipulate the controls. One the user is finished, I would like him to click the save button and save all of the changes to my database (via server-side click event). The problem, obviously, is that when the use clicks "save" the page posts back and I lose all of the javascript changes. For simplicity, I'll layout the save button and one control.

    HTML:
    Code:
    <form id="form1" runat="server">
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Button ID="saveGames" runat="server" CssClass="saveGamesLbtn" OnClick="saveGames_Click" Text="Save"/>
                </ContentTemplate>
            </asp:UpdatePanel>
     
            <div id="controls" runat="server">
                
            </div>
    </form>
    Page_Load:
    Code:
    if (!IsPostBack)
    {
        var games = {grab games from DB via LINQ};
        foreach (var game in games)
        {
            UpdatePanel uPanel = new UpdatePanel();
            uPanel.ID = "gamePanel_" + game.GameID.ToString();
        
            Label xpRewardLbl = new Label();
            xpRewardLbl.ID = "xpRewardLbl_" + game.GameID.ToString();
            xpRewardLbl.Text = "0xp";
     
            uPanel.ContentTemplateContainer.Controls.Add(xpRewardLbl);
            controls.controls.add(uPanel);
        }
    }
    On the "save" click, I want to parse through all labels to grab their changed state but the controls are being removed because their not reloaded in postback - if I reload them in postback, their state is lost. If I could parse through all controls and save the data BEFORE postback, that would be fine too. How do you go about achieving this? I'm thinking something to do with the updatepanels will help me but i'm not sure how to do it. Thanks!
  2. #2
  3. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,527
    Rep Power
    4541
    I don't use asp.NET so can't help with specifics, but I'd look into an ajax-based solution.
    ======
    Doug G
    ======
    I've never been able to appreciate the sublime arrogance of folks who feel they were put on earth just to save other folks from themselves .." - Donald Hamilton

IMN logo majestic logo threadwatch logo seochat tools logo