Saving data

Topics: Developer Forum, Project Management Forum, User Forum
Jun 25, 2012 at 5:26 PM
Edited Jun 25, 2012 at 5:28 PM

I have everything working as it should except I went a little further and added a button to each row which allows me to delete the contents of the textbox of the first column and shift the contents of each texbox in that row to the left by one column.  When I do this, I then update the last textbox with a new value and hit the Save button.  However, when I hit the save button, that row gets saved fine but everything else I changed throughout the rest of the gridview does not save.  I have tried a Javascript which would call the Save button first before shifting the cells, but that doesn't seem to work either.  Does anyone have any idea how I can save the contents of the gridview first before shifting the contents to the left for a particular row?

ASPX

<script type="text/javascript"> 
        function ClickButton() 
        { 
            document.getElementById('SaveButton').click(); 
        } 
</script> 
 
<asp:Button ID="SaveButton" runat="server" Text="Update" /> 
        <Grids:BulkEditGridView ID="EditableGrid" runat="server" AllowSorting="True" AutoGenerateColumns="False" 
            DataKeyNames="Id" DataSourceID="ScoresDataSource" OnRowDataBound="EditableGrid_RowDataBound" 
            SaveButtonID="SaveButton" OnRowCommand="EditableGrid_RowCommand"> 
            <Columns> 
                <asp:TemplateField HeaderText="Action"> 
                    <ItemTemplate> 
                        <asp:Button ID="ShiftScore" runat="server" Text="Drop Oldest Round" OnClientClick="ClickButton()" 
                            CommandName="Shift" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" 
                            Enabled='<%# Eval("Score5").ToString() != "0" %>' /> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField Visible="False"> 
                    <ItemTemplate> 
                        <asp:TextBox ID="IdTextBox" runat="server" Text='<%# Bind("Id") %>' ReadOnly="true"></asp:TextBox> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Name" SortExpression="Member.LastName" > 
                    <ItemTemplate> 
                        <%# Eval("Member.LastName") %>, 
                        <%# Eval("Member.FirstName") %> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Current Member" SortExpression="Member.CurrentMember"> 
                    <ItemTemplate> 
                        <asp:Label ID="lblCurrentMember" runat="server" Text='<%# Eval("Member.CurrentMember") %>'></asp:Label> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Best Score" SortExpression="BestScore"> 
                    <ItemTemplate> 
                        <asp:TextBox ID="BestScoreTextBox" runat="server" MaxLength="3" Text='<%# Bind("BestScore") %>'></asp:TextBox> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Round 1" SortExpression="Score1"> 
                    <ItemTemplate> 
                        <asp:TextBox ID="Score1TextBox" runat="server" MaxLength="3" Text='<%# Bind("Score1") %>'></asp:TextBox> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Round 2" SortExpression="Score2"> 
                    <ItemTemplate> 
                        <asp:TextBox ID="Score2TextBox" runat="server" MaxLength="3" Text='<%# Bind("Score2") %>'></asp:TextBox> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Round 3" SortExpression="Score3"> 
                    <ItemTemplate> 
                        <asp:TextBox ID="Score3TextBox" runat="server" MaxLength="3" Text='<%# Bind("Score3") %>'></asp:TextBox> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Round 4" SortExpression="Score4"> 
                    <ItemTemplate> 
                        <asp:TextBox ID="Score4TextBox" runat="server" MaxLength="3" Text='<%# Bind("Score4") %>'></asp:TextBox> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Round 5" SortExpression="Score5"> 
                    <ItemTemplate> 
                        <asp:TextBox ID="Score5TextBox" runat="server" MaxLength="3" Text='<%# Bind("Score5") %>'></asp:TextBox> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Handicap"> 
                    <ItemTemplate> 
                        <asp:Label ID="lblHandicap" runat="server"></asp:Label> 
                    </ItemTemplate> 
                </asp:TemplateField> 
            </Columns> 
        </Grids:BulkEditGridView>
Code Behind
public partial class Management_ScoresAdmin : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            EditableGrid.Sort("Member.LastName", SortDirection.Ascending); 
        } 
    } 
 
     
    public class BulkEditGridView : System.Web.UI.WebControls.GridView 
    { 
        protected override GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState) 
        { 
            return base.CreateRow(rowIndex, dataSourceIndex, rowType, rowState | DataControlRowState.Edit); 
        } 
 
        [IDReferenceProperty(typeof(Control))] 
        public string SaveButtonID 
        { 
            get 
            { 
                string val = (string)this.ViewState["SaveButtonID"]; 
                if (val == null) 
                { 
                    return string.Empty; 
                } 
                return val; 
            } 
            set 
            { 
                this.ViewState["SaveButtonID"] = value; 
            } 
        } 
 
        protected override void OnLoad(EventArgs e) 
        { 
            base.OnLoad(e); 
 
            //Attach an event handler to the save button. 
            if (false == string.IsNullOrEmpty(this.SaveButtonID)) 
            { 
                Control btn = RecursiveFindControl(this.NamingContainer, this.SaveButtonID); 
                if (null != btn) 
                { 
                    if (btn is Button) 
                    { 
                        ((Button)btn).Click += new EventHandler(SaveClicked); 
                    } 
                    else if (btn is LinkButton) 
                    { 
                        ((LinkButton)btn).Click += new EventHandler(SaveClicked); 
                    } 
                    else if (btn is ImageButton) 
                    { 
                        ((ImageButton)btn).Click += new ImageClickEventHandler(SaveClicked); 
                    } 
 
                    //add more button types here. 
                } 
            } 
        } 
 
        private Control RecursiveFindControl(Control namingcontainer, string controlName) 
        { 
            Control c = namingcontainer.FindControl(controlName); 
 
            if (c != null) 
                return c; 
 
            if (namingcontainer.NamingContainer != null) 
                return RecursiveFindControl(namingcontainer.NamingContainer, controlName); 
 
            return null; 
        } 
 
        private void SaveClicked(object sender, EventArgs e) 
        { 
            this.Save(); 
            this.DataBind(); 
        } 
 
        protected override void InitializeRow(GridViewRow row, DataControlField[] fields) 
        { 
            base.InitializeRow(row, fields); 
            foreach (DataControlFieldCell cell in row.Cells) 
            { 
                if (cell.Controls.Count > 0) 
                { 
                    AddChangedHandlers(cell.Controls); 
                } 
            } 
        } 
 
        private void AddChangedHandlers(ControlCollection controls) 
        { 
            foreach (Control ctrl in controls) 
            { 
                if (ctrl is TextBox) 
                { 
                    ((TextBox)ctrl).TextChanged += new EventHandler(this.HandleRowChanged); 
                } 
                else if (ctrl is CheckBox) 
                { 
                    ((CheckBox)ctrl).CheckedChanged += new EventHandler(this.HandleRowChanged); 
                } 
                else if (ctrl is DropDownList) 
                { 
                    ((DropDownList)ctrl).SelectedIndexChanged += new EventHandler(this.HandleRowChanged); 
                } 
            } 
        } 
 
        void HandleRowChanged(object sender, EventArgs args) 
        { 
            GridViewRow row = ((Control)sender).NamingContainer as GridViewRow; 
            if (null != row && !dirtyRows.Contains(row.RowIndex)) 
            { 
                dirtyRows.Add(row.RowIndex); 
            } 
        } 
 
        private List<int> dirtyRows = new List<int>(); 
 
        public void Save() 
        { 
            foreach (int row in dirtyRows) 
            { 
                this.UpdateRow(row, false); 
            } 
 
            dirtyRows.Clear(); 
        } 
    } 
 
    // Control the Shift Button in the Scores GridView 
    protected void EditableGrid_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
        if (e.CommandName == "Shift") 
        { 
            // Retrieve the row index stored in the  
            // CommandArgument property. 
            int index = Convert.ToInt32(e.CommandArgument); 
 
            // Retrieve the row that contains the button  
            // from the Rows collection. 
            GridViewRow row = EditableGrid.Rows[index]; 
 
            string s2 = ((TextBox)row.FindControl("Score2TextBox")).Text; 
            string s3 = ((TextBox)row.FindControl("Score3TextBox")).Text; 
            string s4 = ((TextBox)row.FindControl("Score4TextBox")).Text; 
            string s5 = ((TextBox)row.FindControl("Score5TextBox")).Text; 
 
            TextBox newScore1 = (TextBox)row.FindControl("Score1TextBox"); 
            TextBox newScore2 = (TextBox)row.FindControl("Score2TextBox"); 
            TextBox newScore3 = (TextBox)row.FindControl("Score3TextBox"); 
            TextBox newScore4 = (TextBox)row.FindControl("Score4TextBox"); 
            TextBox newScore5 = (TextBox)row.FindControl("Score5TextBox"); 
 
            newScore1.Text = s2; 
            newScore2.Text = s3; 
            newScore3.Text = s4; 
            newScore4.Text = s5; 
            newScore5.Text = null; 
 
            // Sets focus on the Round 5 textbox 
            Control c = new Control(); 
            c = (TextBox)row.FindControl("Score5TextBox"); 
            this.SetFocus(c); 
        } 
    } 
 }
Jun 26, 2012 at 9:41 PM

Resolved...

I figured out how to do the shifting using Javascript on the client side instead and now I am able to shift the cells left without effecting the rest of the changes.