2013-09-25 6 views
0

Я хочу сохранить поле Gridview одним щелчком мыши. Можно ли получить все данные?Сохраните данные 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; } 
    } 
} 

Можно ли принимать изменения решения от яваскрипта и сохранить его с главной страницы ..

+0

Какой метод вы планируете использовать для сохранения данных? DataSet - простой способ. – RealityDysfunction

+0

в Button1_Click .... не могли бы вы объяснить. –

ответ

1

Вы можете зациклить строки и получить поле адреса для каждой строки.

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

    //Update your DB here 
} 

Вызвать метод LoadData здесь

Смежные вопросы