~辑某一个独立的GridView单元根{?/strong>

我所演示的这个GridView有一个不可见的asp:ButtonField控gQ它处于GridView的第一列,名ؓ“SingleClick”?nbsp;它用于给GridView的数据行增加单击事g?nbsp;
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False" />
</Columns>
其它每一列的ItemTemplate中有一个可见的Label控g和一个不可见的TextBox或DropDownList控g?nbsp;Z方便Q我们称Label为显C控ӞTextBox或DropDownList为编辑控件?br>
<asp:TemplateField HeaderText="Task">
<ItemTemplate>
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
<asp:TextBox ID="Description" runat="server" Text='<%# Eval("Description") %>' Width="175px" visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
q里的办法就是用昄控g来显C数据,当单元格所包含的显C控件被单击的时候,则把昄控g的Visible属性设|ؓfalseq且把编辑控件的Visible属性设|ؓtrue?nbsp;q里不用使用EditItemTemplat?nbsp;
在RowDataBound事g内@环ؓ每一数据行的每一单元格增加单M件?nbsp;使用单元格在数据行中的烦引作Z件参敎ͼq样在单元格触发了单M件后我们可以知道到底是哪个单元D单击了?nbsp;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 从第一个单元格内获得LinkButton控g
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
// q回一个字W串Q表C对包含目标控g?nbsp;ID 和事件参数的回发函数?nbsp;JavaScript 调用
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
// l每一个可~辑的单元格增加事g
for (int columnIndex = _firstEditCellIndex; columnIndex < e.Row.Cells.Count; columnIndex++)
{
// 增加列烦引作Z件参?br> string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString());
// l单元格增加onclick事g
e.Row.Cells[columnIndex].Attributes["onclick"] = js;
// l单元格增加鼠标l过时指针样?br> e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
}
}
}
在RowCommand事g内读出命令参数和事g参数?nbsp;q会告诉我们被选中的行和列的烦引?nbsp;
int _rowIndex = int.Parse(e.CommandArgument.ToString());
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
因ؓ知道了被选中的行和列的烦引,所以可以通过把显C控件的Visible讄为falseQ编辑控件的Visible讄为true来把某个独立的单元格讄为编辑模式?nbsp;然后通过清除单元格的属性来删除被选中单元格的单击事g?nbsp;
// 获得被选中单元格的昄控gq设|其不可?br> Control _displayControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[1];
_displayControl.Visible = false;
// 获得被选中单元格的~辑控gq设|其可见
Control _editControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[3];
_editControl.Visible = true;
// 清除被选中单元格属性以删除click事g
_gridView.Rows[_rowIndex].Cells[_columnIndex].Attributes.Clear();
下面有一些代码用于回发服务器后设|焦点到~辑控gQ如果编辑控件是DropDownList的话Q那么它的SelectedValue要设|ؓ昄控g的|如果~辑控g是TextBox的话Q那么ؓ了做好编辑的准备p使它的文本被选中?nbsp;
// 讄焦点到被选中的编辑控?br> ClientScript.RegisterStartupScript(GetType(), "SetFocus",
"<script>document.getElementById('" + _editControl.ClientID + "').focus();</script>");
// 如果~辑控g是DropDownList的话
// SelectedValue讄为显C控件的?br> if (_editControl is DropDownList && _displayControl is Label)
{
((DropDownList)_editControl).SelectedValue = ((Label)_displayControl).Text;
}
// 如果~辑控g是TextBox的话则选中文本框内文本
if (_editControl is TextBox)
{
((TextBox)_editControl).Attributes.Add("onfocus", "this.select()");
}
在这个Demo中,我把事g被触发的历史记录也写C里?nbsp;
如果GridView处于~辑模式的话Q那么要在RowUpdating事g里去查找被选中行的每一个单元格?nbsp;如果发现单元格处于编辑模式的话,那么p?#8220;更新”代码?nbsp;在这个Demo中,数据保存在DataTable里,而这个DataTable则储存在session中?nbsp;
// 循环每一列以扑ֈ处于~辑模式下的单元?br> for (int i = 1; i < _gridView.Columns.Count; i++)
{
// 获得单元格的~辑控g
Control _editControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[3];
if (_editControl.Visible)
{
. update the data
}
}
Z保RowUpdating事g在编辑单元格后被Ȁ发,要在Page_Load中来触发q个事g?nbsp;~辑了TextBox后,通过按回车键或者单d一单元格来佉K面做回发处理Q下面的q段代码是用于保M数据的改变都会被更新?br>
if (this.GridView1.SelectedIndex > -1)
{
this.GridView1.UpdateRow(this.GridView1.SelectedIndex, false);
}
Z验证而注册回发和回调数据
在RowDataBound中创建的自定义事件必要在页中注册?nbsp;通过重写RenderҎ来调用ClientScriptManager.RegisterForEventValidation?nbsp;通过GridViewRow.UniqueIDq回行的唯一IDQ按U的唯一ID通过在行的唯一ID后附?#8220;$ct100”而生成?nbsp;
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
for (int columnIndex = _firstEditCellIndex; columnIndex < r.Cells.Count; columnIndex++)
{
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00", columnIndex.ToString());
}
}
}
base.Render(writer);
}
q将防止M“回发或回调参数无?#8221;的错误?br>
q个Demo中的其它CZ
使用SQL数据源控件编辑某一独立的GridView单元?br>用SqlDataSouce控g实现q个技术需要对GridView的RowUpdating事g做一些修攏V?nbsp;当更新GridView的行的时候,SqlDataSource控g一般要把|valuesQ从EditItemTemplate转移到NewValues集合里?nbsp;因ؓ我们没有使用EditItemTemplateQ所以这U情况下|valuesQ不会自动地转移到NewValues集合里?nbsp;
e.NewValues.Add(key, value);
我在App_Data文g夹下使用了一个简单的SQL Server Express数据库?nbsp;Q要使用你自q数据库的话,你可以修改web.config里的q接字符Ԍ
使用对象数据源控件编辑某一独立的GridView单元?br>本示例用了App_Code文g夹内的两个类Q?br> ·Task.cs – d对象
·TaskDataAccess.cs – 理d对象
Aspx늚后置代码与SQL Data SourceCZ是一L?nbsp;ObjectDataSource通过TaskDataAccess.csc里的GetTasks和UpdateTaskҎ来管理数据?nbsp;
有着电子数据表样式的GridView
q里有一个与电子数据表的样式很像的GridView?nbsp;Q虽然它看v来像一个电子数据表Q但是ƈ不是真的有像电子数据表一L功能Q它仍然是一个GridView。)
q里虽然有一些单d改变单元格样式的附加代码Q但是主要的代码q是与上面所q是相同的?br>
用SQL数据源控件实现有着电子数据表样式的GridView
本示例与上面的基本相同,但是它修改了GridView的RowUpdating事g以其允许用SqlDataSource控g来工作?br>
参?/strong>
·GridView和DataList响应单击数据行和双击数据行事?/font>
·ASP.NET 2.0数据教程
l论
如果你想在GridView中一ơ只针对一个单元格q行~辑Q那么这个方法将会对你有所帮助?br>
译者注Q事仉证(EventValidationQ。出于安全目的,此功能验证回发或回调事g的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效ƈ且是预期的,则用ClientScriptManager.RegisterForEventValidationҎ来注册回发或回调数据以进行验证?br>
点击下蝲此文?/font>

]]>