您的位置:首页 > 编程语言 > ASP

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: "));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: