将Gridview数据保存在按钮中,然后再次加载?

时间:2013-09-25 16:43:06

标签: c# javascript asp.net gridview grid

我想在单击按钮中保存Gridview字段。是否有可能获得所有数据?

我尝试过的事情

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="GridViewCellEdit._Default" enableEventValidation="false" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript">

        function GetSelectedRow(lnk) {
            debugger;
            var row = lnk.parentNode.parentNode;
            var rowIndex = row.rowIndex - 1;
            alert("row: " + rowIndex);
            return false;
        }
    </script>
    <asp:Panel ID="pnl1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button Save" OnClick="Button1_Click" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" OnRowDataBound="GridView1_RowDataBound">
        </asp:GridView>
    </asp:Panel>
</asp:Content>

所有列都是自动生成的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GridViewCellEdit
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MyEntity entity = new MyEntity();
            List<MyEntity> list = new List<MyEntity>();
            list.Add(new MyEntity() { ID = 1,Name="Name1",Address="It is very long field so no more button in grid1"});
            list.Add(new MyEntity() { ID = 2, Name = "Name2", Address = "It is very long field so no more button in grid2" });
            list.Add(new MyEntity() { ID = 3, Name = "Name3", Address = "It is very long field so no more button in grid3" });
            list.Add(new MyEntity() { ID = 4, Name = "Name4", Address = "It is very long field so no more button in grid4" });
            list.Add(new MyEntity() { ID = 4, Name = "Name5", Address = "It is very long field so no more button in grid5" });
            list.Add(new MyEntity() { ID = 6, Name = "Name6", Address = "It is very long field so no more button in grid6" });
            list.Add(new MyEntity() { ID = 7, Name = "Name7", Address = "It is very long field so no more button in grid7" });
            list.Add(new MyEntity() { ID = 8, Name = "Name8", Address = "It is very long field so no more button in grid8" });
            list.Add(new MyEntity() { ID = 9, Name = "Name9", Address = "It is very long field so no more button in grid9" });
            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 e.Row.Attributes.Add("onclick", "this.style.backgroundColor='orange'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
            e.Row.Cells[2].Width = new Unit("700px");
            TextBox txtAddress = new TextBox();
            txtAddress.ReadOnly = false;
            txtAddress.Style.Add("width", "99%");
            e.Row.Cells[2].Controls.Add(txtAddress);
            e.Row.Cells[2].Style.Add("text-align", "center");
            txtAddress.Text = e.Row.Cells[2].Text;
            txtAddress.Attributes.Add("onblur", "return GetSelectedRow(this); ");
            GridView1.Attributes.Add("style", "table-layout:fixed");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Setp1 : do some trick to get the address 
            //Step2 : Check if address is changed as only address field is editable
            //Step3 : Update database for all the changed address in a loop
            //Setp4 : reload the grid with updated Data
        }
    }
    public class MyEntity
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
    }
}

是否可以从javascript获取更改地址并将其保存在主页面中。

1 个答案:

答案 0 :(得分:1)

您可以循环行并获取每行的地址字段。

foreach (GridViewRow row in GridView1.Rows)
{
    string address = row.Cells[2].Text;
    string name = row.Cells[1].Text;

    //Update your DB here
}

在此处调用LoadData方法