asp.net GridView的使用
时间:2010-07-16 来源:cuisuqiang
光棒效果
在GridView的RowCreated的事件里,即数据行创建时的事件
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes.Add("onmouseover","currertcolor=this.style.backgroundColor;this.style.backgroundColor='c0c0ff';this.style.cursor='hand'"); //鼠标移动上去时记录当前颜色,改变当前颜色,并将鼠标显示为小手
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currertcolor");
}
根据数据将制定数据行或单元格操作
在GridView的RowDataBound即数据绑定事件中
if (e.Row.RowType == DataControlRowType.DataRow) {
if (e.Row.Cells[4].Text == "女"){
//e.Row.BackColor = System.Drawing.Color.Red;
e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
}
}//如果第五个单元格字段为女则可以改变该单元格颜色或该行的颜色
子页面返回数据到母页面
母页面为一个简单HTML页面
<input id="Button1" type="button" onclick="return openpage('GridViewClient.aspx')" value="button" />
<input id="name" type="text" />
<script language="javascript" type="text/javascript">
function openpage(htmlurl) {
var newwin=window.open(htmlurl,"newWin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes,top=200,left=200,width=650,height=400");
newwin.focus();
return false;
}
</script>
<!--通过按钮打开一个格式化的窗口,页面里的文本框用于让户子窗体赋值-->
子页面为asp页面,在GridView的RowDataBound即数据绑定事件中
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes.Add("ondblclick", "ReKey('" + e.Row.Cells[2].Text + "')");
}
页面上有脚本函数
<script language="javascript" type="text/javascript">
function ReKey(k) {
window.opener.document.getElementById('name').value=k;
window.close();
}
</script>
<!--window.opener为该找到父窗体中的文本框并赋值-->
服务器端全选
<asp:TemplateField>
<HeaderTemplate>
选中全部<asp:CheckBox ID="checkAll" runat="server" OnCheckedChanged="checkAll_CheckedChanged" AutoPostBack="True" />
</HeaderTemplate>
<ItemTemplate>
选择<asp:CheckBox ID="checkThis" runat="server" />
</ItemTemplate>
</asp:TemplateField>
checkAll的AutoPostBack必须设置否则没有效果
服务器端事件
protected void checkAll_CheckedChanged(object sender, EventArgs e) {
if (((CheckBox)sender).Checked) {
for (int i = 0; i < GridView1.Rows.Count; i++){
((CheckBox)GridView1.Rows[i].FindControl("checkThis")).Checked = true;
}
}
else{
for (int i = 0; i < GridView1.Rows.Count; i++){
((CheckBox)GridView1.Rows[i].FindControl("checkThis")).Checked = false;
}
}
}
GridView导出Excel
在一个按钮事件里处理即可
protected void Button1_Click(object sender, EventArgs e) {
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{ }
注意:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
如果启用了分页的话调用的时候就会报错
只能在执行 Render() 的过程中调用 RegisterForEventValidation;
那是因为没有写要覆盖的方法,服务器认为客户端不能调用服务器端