asp.net学习之GridView事件、GridViewRow对象
2010-09-17 13:54
543 查看
1. GridView控件的事件
GridView有很多事件,事件可以定制控件的外观或者行为。事件分为三类1.1 GridView显示数据时的事件
● DataBinding : 在绑定数据源之前触发 [继承自Control]
● DataBound 在绑定到数据源后触发
● RowCreated 创建每一行时触发
● RowDataBound : 每一行绑定完数据时触发
MSDN解释:呈现 GridView 控件之前,必须先为该控件中的每一行创建一个 GridViewRow 对象。在创建 GridView 控件中的每一行时,将引发 RowCreated 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时就执行一个自定义例Code
<script runat="server">
void AuthorsGridView_RowDataBound (Object sender, GridViewRowEventArgs e)
{
// 是否处于编辑模式
if(e.Row.RowState == DataControlRowState.Edit) {
DataRowView rowView = (DataRowView)e.Row.DataItem; // 获得编辑行的DataRowView对象
String state = rowView["state"].ToString(); // 通过DataRowView,可以直接取出某字段
DropDownList list = (DropDownList)e.Row.FindControl("StatesList"); // 获得StatesList的DropDownList对象
ListItem item = list.Items.FindByText(state); // 找到DropDonList中的某一项
list.SelectedIndex = list.Items.IndexOf(item); // 选中那一项
}
}
void AuthorsGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = AuthorsGridView.Rows[AuthorsGridView.EditIndex];
// Retrieve the DropDownList control from the row.
DropDownList list = (DropDownList)row.FindControl("StatesList");
e.NewValues["state"] = list.SelectedValue;
}
</script>
<asp:gridview id="AuthorsGridView" datasourceid="AuthorsSqlDataSource"
autogeneratecolumns="false" autogenerateeditbutton="true" datakeynames="au_id"
onrowdatabound="AuthorsGridView_RowDataBound"
onrowupdating="AuthorsGridView_RowUpdating" runat="server">
<columns>
<asp:boundfield datafield="au_lname" headertext="Last Name"/>
<asp:boundfield datafield="au_fname" headertext="First Name"/>
<asp:templatefield headertext="State">
<itemtemplate> <%#Eval("state")%> </itemtemplate>
<edititemtemplate>
<asp:dropdownlist id="StatesList" datasourceid="StatesSqlDataSource"
datatextfield="state" runat="server"/>
<asp:sqldatasource id="StatesSqlDataSource" <!-- 在GridView模板中也可以加入SqlDataSource控件 -->
selectcommand="SELECT Distinct [state] FROM [authors]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server"> </asp:sqldatasource>
</edititemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]"
updatecommand="UPDATE authors SET [au_lname]=@au_lname, [au_fname]=@au_fname, [state]=@state WHERE au_id=@au_id"
connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server">
</asp:sqldatasource>
● Cells属性: 通过使用 Cells 属性,可以访问 GridViewRow 对象的单独单元格.
如果某个单元格包含其他控件,则通过使用单元格的 Controls 集合,可以从单元格检索控件。
如果某列是BoundField字段,可以使使用Cells[].Text属性。
【注:在 TemplateField 字段列中可以直接使用数据绑定表达式,无需将值绑定到控件的某个属性。在这种情况下,字段值将自动放置在 DataBoundLiteralControl 控件中。若要检索字段值,必须先从相应单元格检索 DataBoundLiteralControl 控件,然后再使用其 Text 属性。例如:】
<script runat="server">
void AuthorsGridView_SelectedIndexChanged(Object sender, EventArgs e) {
String lastName = selectRow.Cells[1].Text; // 针对BoundField字段
DataBoundLiteralControl firstNameLiteral = (DataBoundLiteralControl)selectRow.Cells[2].Controls[0]; //针对TemplateField字段
String firstName = firstNameLiteral.Text;
}
</script>
<asp:gridview id="AuthorsGridView" datasourceid="AuthorsSqlDataSource"
autogeneratecolumns="false" autogenerateselectbutton="true"
onselectedindexchanged="AuthorsGridView_SelectedIndexChanged" runat="server">
<columns>
<asp:boundfield datafield="au_lname" headertext="Last Name"/>
<asp:templatefield headertext="FirstName">
<itemtemplate> <%#Eval("au_fname")%> </itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
2.4 其它一些属性
GridViewRow有很多属性,具体可以参考MSDN,
它包括了一些用于改变样式的属性,这些属性继承自WebControl,
如: BorderColor,BorderStyle,BackColor,ControlStyle,CssClass,Font,ForColor,Height,Width..
另外,也有Attributes,Controls,Context,Event,Page,Parent,TemplateControl,ViewState等继承Control的属性
2.5 GirdViewRow对象的一些方法
方法也很多,需要时参考MSDN,常用的,包括:
FindControl,HasControl,ClearChildControlState…
3. TableCell 对象
TableCell对象表示 Table 控件中的单元格。通过GridViewRow.Cells对象就是返回的TablelCell的集合。对于WEB来说,其就是一个<td></td>
该对象有一些常用的属性,如Text,Controls.RowSpan,ToolTip,VerticalAlign,HorizontalAlign…属性
关于Control对象,MSDN上有一些说明:
在 ASP.NET 页上,当以声明方式在服务器控件的开始标记和结束标记之间添加控件时,ASP.NET 会自动将这些控件添加到包含服务器控件的 ControlCollection 中。任何不在服务器上处理的 HTML 标记或者文本字符串都视为 LiteralControl 对象。它们像其他服务器控件一样被添加到集合中。
Controls 属性允许编程访问任何服务器控件的 ControlCollection 类实例。您可以向集合添加控件、从集合中移除控件,或者循环访问集合中的服务器控件。
Controls.Add(new LiteralControl("<h3>Value: "));
相关文章推荐
- asp.net学习之GridView事件、GridViewRow对象
- asp.net学习之GridView事件、GridViewRow对象
- asp.net学习之GridView事件、GridViewRow对象
- 在学习ASP.NET中,GridView 控件的RowDataBound事件的使用详解
- 菜鸟学Asp.Net——GridView控件之RowDataBind事件中如何获取已绑定的数据
- ASP.NET DEMO 5:如何在GridView的RowCommand事件中获取当前的GridViewRow
- asp.net中GridView内部点击事件判断是第几行并获取row数据
- ASP.NET GridView_第五篇_用途多多的 OnRowDataBound 事件
- [asp.net]Application对象与PageRequestManager对象的事件顺序
- [习题]如何触发 GridView 身体里面的「子控件」的事件 (ASP.NET案例精编 / 清华大学出版社 Ch.10/11两章的补充)
- How to get the Values of Selected Row from a Gridview using ASP.NET
- 黑马程序员之 ASP.NET学习笔记:GridView控件的常用属性及方法
- asp.net 学习之路之gridView控件
- asp.net学习之GridView七种字段
- Asp.net 2.0 GridView的几个事件(如实现: 行的双击/单击/捕捉键盘按键/鼠标悬浮/移出效果)(示例代码下载)
- 【Asp.Net】GridView中的各种事件
- GridView(Asp.net)事件过程详解记录
- asp.net 2.0 gridview 分页 跳转 警告窗口 注册事件
- Asp.net 2.0 自定义控件开发[创建自定义HeaderRow的GridView控件][示例代码下载]
- 有关asp.net的Application对象的学习