前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebDataGridSelectRow.aspx.cs" Inherits="BasisFile_WebDataGridSelectRow" %> <%@ Register Assembly="Infragistics4.Web.v15.1, Version=15.1.20151.1018, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" Namespace="Infragistics.Web.UI.GridControls" TagPrefix="ig" %> <%@ Register Assembly="Infragistics4.Web.v15.1, Version=15.1.20151.1018, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" Namespace="Infragistics.Web.UI.NavigationControls" TagPrefix="ig" %> <%@ Register Assembly="Infragistics4.Web.v15.1, Version=15.1.20151.1018, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" Namespace="Infragistics.Web.UI" TagPrefix="ig" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>后台绑定与选中行</title> <script type="text/javascript" id="igClientScript"> <!-- function WebDataGrid_Selection_RowSelectionChanged(sender, eventArgs) { ///<summary> /// ///</summary> ///<param name="sender" type="Infragistics.Web.UI.WebDataGrid"></param> ///<param name="eventArgs" type="Infragistics.Web.UI.RowSelectionChangedEventArgs"></param> //Add code to handle your event here. var rowindex = sender.get_behaviors().get_selection().get_selectedRows(0).getItem(0).get_cellByColumnKey("Data").get_text(); document.getElementById("TextBox1").value = rowindex; } --> </script> </head> <body> <form id="form1" runat="server"> <ig:WebScriptManager ID="WebScriptManager1" runat="server"> </ig:WebScriptManager> <div> <ig:WebDataGrid ID="WebDataGrid" runat="server" Height="350px" Width="100%" DataKeyFields="id" onrowselectionchanged="WebDataGrid_RowSelectionChanged"> <EditorProviders> <ig:TextEditorProvider ID="WebDataGrid1_TextEditorProvider1"> <EditorControl ClientIDMode="Predictable"></EditorControl> </ig:TextEditorProvider> </EditorProviders> <Behaviors> <ig:EditingCore AutoCRUD="false"> <Behaviors> <ig:CellEditing> <ColumnSettings> <ig:EditingColumnSetting EditorID="WebDataGrid1_TextEditorProvider1" ColumnKey="item" /> <%--只有这一行是不可编辑的--%> <ig:EditingColumnSetting ColumnKey="Data" ReadOnly="true" /> </ColumnSettings> </ig:CellEditing> </Behaviors> </ig:EditingCore> <ig:Selection CellClickAction="Row" RowSelectType="Single"> <SelectionClientEvents RowSelectionChanged="WebDataGrid_Selection_RowSelectionChanged" /> <AutoPostBackFlags RowSelectionChanged="True" /> </ig:Selection> </Behaviors> </ig:WebDataGrid> <asp:Button ID="btnDisableEditing" runat="server" OnClick="Button1_Click" Text="所有不可编辑" /> <br /> 选中行的第一列的值:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> </div> </form> </body> </html>
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class BasisFile_WebDataGridSelectRow : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Session["key"] = WebDataGrid.DataSource = MakeTable(); WebDataGrid.DataBind(); } WebDataGrid.DataSource = Session["key"]; } private DataTable MakeTable() { // Create a new DataTable. System.Data.DataTable table = new DataTable("Table"); // Declare variables for DataColumn and DataRow objects. DataColumn column; DataRow row; // Create new DataColumn, set DataType, column = new DataColumn(); // ColumnName and add to DataTable. column.DataType = System.Type.GetType("System.Guid"); //System.Guid.NewGuid().ToString() ; //column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "id"; column.ReadOnly = true; // Add the Column to the DataColumnCollection. table.Columns.Add(column); // Create second column. column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "Item"; column.AutoIncrement = false; column.Caption = "Item"; column.ReadOnly = false; column.Unique = false; // Add the column to the table. table.Columns.Add(column); // Create third column. column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "Data"; column.AutoIncrement = false; column.Caption = "Data"; column.ReadOnly = false; column.Unique = false; // Add the column to the table. table.Columns.Add(column); // Make the ID column the primary key column. DataColumn[] PrimaryKeyColumns = new DataColumn[1]; PrimaryKeyColumns[0] = table.Columns["id"]; table.PrimaryKey = PrimaryKeyColumns; for (int i = 0; i <= 20; i++) { row = table.NewRow(); row["id"] = System.Guid.NewGuid(); row["Item"] = "Item " + i; row["Data"] = "Data " + i; table.Rows.Add(row); } return table; } protected void Button1_Click(object sender, EventArgs e) { //EditingCore中包含行编辑 列编辑等如下 //WebDataGrid.Behaviors.EditingCore.Enabled = false; //CellEditing不可用 WebDataGrid.Behaviors.EditingCore.Behaviors.CellEditing.Enabled = false; //RowEditing不可用 //WebDataGrid.Behaviors.EditingCore.Behaviors.RowEditing.Enabled = false; } protected void WebDataGrid_RowSelectionChanged(object sender, Infragistics.Web.UI.GridControls.SelectedRowEventArgs e) { string a = e.CurrentSelectedRows[0].DataKey[0].ToString(); //该方法可以获取关键值 但不能修改其他控件值 //该语句运行后TextBox1不显示值 //TextBox1.Text = a; //解决方法一:设置表格enable ajax=false //二:用前台事件比较好 } }