Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Dynamic controls and asp:button postback

c# asp.net ajax javascript postback sql

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 Tchpowdog

Tchpowdog

    CC Regular

  • Member
  • PipPipPip
  • 44 posts

Posted 12 June 2016 - 11:22 AM

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:

<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:

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!


Edited by Tchpowdog, 12 June 2016 - 11:34 AM.