1):GridView中加载bool类型的sex显示问题
<%#bool.Parse(Eval("Sex").ToString()) == true ? "男" : "女" %>
2):利用普通checkbox 控件来制作全选功能,删除时的代码
string deleteStr = Request["group1"]; //获取选中的checkbox集合
SqlCommand cmd = new SqlCommand("delete from ShoppingCart where productId in (" + deleteStr + ")", conn);//注意此处不能用
cmd.Parameters.AddWithValue("@deleteStr", deleteStr);
//前提是先绑定 checkbox的 value值
<input id="Checkbox1" type="checkbox" name="group1" value='<%#Eval("productId") %>' />
实现全选Javascript代码: 有checkBox2调用此方法
function getCheckAll(chk)
{
var items=document.getElementsByTagName("Input");
for(i=0;i<items.length;i++)
{
if(items[i].type=="checkbox")
items[i].checked=chk.checked;
}
}
3):实现光棒效果与合并单元格
protected void gvMain_RowCreated(object sender, GridViewRowEventArgs e)
{
//实现光棒效果
if (e.Row.RowType == DataControlRowType.DataRow)//给行添加事件
{
e.Row.Attributes.Add("onmouseover","this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=''");
}
//合并单元格
else if (e.Row.RowType == DataControlRowType.Footer)//给页脚添加内容
{
int count = e.Row.Cells.Count;//获得页脚的例数
for (int i = count-2;i>0; i--)//不删除第一格和最后一格
{
e.Row.Cells.RemoveAt(i);
}
//合并单元格,把除最后一格的weight加到第一格
e.Row.Cells[0].ColumnSpan = count - 1;
//为第一格设置文本对齐方式
e.Row.Cells[0].Style.Add("text-align", "left");
//用于显示总金额
Literal lblTotal = new Literal();
lblTotal.ID = "lblTotal";
lblTotal.Text ="总金额"+ string.Format("{0:C}",(BLL.ShoppingCartManager.SelectTotal()));
int newCount = e.Row.Cells.Count;
e.Row.Cells[newCount-1].Controls.Add(lblTotal);
e.Row.Cells[newCount-1].Style.Add("text-align", "right");
}
}
4:) 把GridView控件中的数据导入到Excel中去
1, 首先要把GridView页面 EnableEventValidation="false",对应按钮的Click事件加上如下代码:
Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName);//FileName为文件下载名称 // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = FileType;//设置输出文件类型为excel文件。 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.gvMain.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End();
|