asp.net gridview动态添加列,并获取其数据;

时间:2022-04-11 13:45:16

1,绑定数据前先动态添加列,见方法CreateGridColumn(只在第一次加载动态添加);

2,gvlist_RowDataBound为对应列添加控件;

前台代码:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="modifysetLeaveconfig2.aspx.cs"
Inherits="web.system.modifysetLeaveconfig2" %> <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<link href="/css/default.css" rel="stylesheet" type="text/css" />
<link href="/css/jquery.ui.all.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="/js/jquery.ui.core.js"></script>
<script type="text/javascript" src="/js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/js/jquery.ui.mouse.js"></script>
<script type="text/javascript" src="/js/jquery.ui.draggable.js"></script>
<script type="text/javascript" src="/js/jquery.ui.position.js"></script>
<script type="text/javascript" src="/js/jquery.ui.resizable.js"></script>
<script type="text/javascript" src="/js/jquery.ui.button.js"></script>
<script type="text/javascript" src="/js/jquery.ui.dialog.js"></script>
<script type="text/javascript" src="/js/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script type="text/javascript" src="/js/jquery.validate.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="hfscmcid" />
<asp:HiddenField runat="server" ID="hfplatform" />
<div class="container">
<div class="content">
<div class="cPanel">
<div class="detailPanel">
<asp:GridView ID="gvlist" GridLines="None" runat="server" CellSpacing="0" BorderWidth="0"
CellPadding="0" class="oderListTbl" AutoGenerateColumns="False" EmptyDataText="没有找到数据"
OnRowCommand="gvlist_RowCommand" OnRowDataBound="gvlist_RowDataBound">
<Columns>
<asp:TemplateField ItemStyle-Width="60px">
<HeaderTemplate>
<asp:CheckBox ID="cbAll" runat="server" Text="" onclick="javascript:SelectAll(this)" />
<br />
<asp:LinkButton ID="lbtall" runat="server" OnClientClick="javascript:return confirm('您确定要批量更新该数据吗?');"
CommandName="updateall" CommandArgument='<%# Eval("scmcid")+","+Eval("policytype") %>'>批量更新</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbItem" runat="server" />
<asp:HiddenField ID="hfpolicytype" runat="server" Value='<%# Eval("policytype")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="政策类型" ItemStyle-Width="50px">
<ItemTemplate>
<%# Eval("name")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="状态" ItemStyle-Width="50px">
<ItemTemplate>
<asp:LinkButton ID="lbtModify" runat="server" CommandName="udtallstate" CommandArgument='<%# Eval("scmcid")+","+Eval("policytype") %>'
ToolTip="点击更改状态" Text="启/禁用"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div class="pager">
<webdiyer:AspNetPager ID="pager" CssClass="paginator" CurrentPageButtonClass="cpb"
runat="server" AlwaysShow="True" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页"
PageSize="1000000" PrevPageText="上一页" ShowCustomInfoSection="Left" ShowInputBox="Never"
OnPageChanging="pager_PageChanging" CustomInfoTextAlign="Left" LayoutType="Table"
CustomInfoHTML="总条数:%RecordCount% 当前页数 %CurrentPageIndex% of %PageCount%">
</webdiyer:AspNetPager>
</div>
</div>
</div>
</div>
<div class="clr">
</div>
</div>
</form>
<script type="text/javascript"> $(function () {
$("#gvlist tr:odd").addClass("highLight");
});
function SelectAll(aControl) {
var tempControl = aControl;
var isChecked = tempControl.checked; elem = tempControl.form.elements;
for (i = 0; i < elem.length; i++)
if (elem[i].type == "checkbox" && elem[i].id != tempControl.id) {
if (elem[i].checked != isChecked)
elem[i].click();
}
}
</script>
</body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using webframework.common;
using web.code; namespace web.system
{
public partial class modifysetLeaveconfig2 : code.BasePage
{
webframework.bll.systemconfiguration.bllsetlog blllog = new webframework.bll.systemconfiguration.bllsetlog();
webframework.bll.systemconfiguration.bllsetLeaveconfig bll = new webframework.bll.systemconfiguration.bllsetLeaveconfig();
protected string platform = "";
protected static int pageIndex = ;
private List<webframework.model.modelsystem> lpolicytype = new List<webframework.model.modelsystem>();
private static List<webframework.model.modelsystem> listshop = new List<webframework.model.modelsystem>();
private static List<webframework.model.modelsetLeaveconfig> listslc = new List<webframework.model.modelsetLeaveconfig>();
private static int times = ;
protected void Page_Load(object sender, EventArgs e)
{
hfplatform.Value = Request.QueryString["platform"];
platform = hfplatform.Value;
hfscmcid.Value = Request.QueryString["id"];
if (!IsPostBack)
{
times = ;
PageRender();
}
//PageRender();
} void PageRender()
{
listshop = code.CacheCollection.GetSystem("", platform);//动态添加列
pageIndex = pageIndex > ? pageIndex : pager.CurrentPageIndex;
string sql = code.SqlHelper.GetTableByPager("select name,svalue AS [policytype],-1 AS [id], " + hfscmcid.Value.ToString() + " AS [scmcid] " + (listshop.Count == ? "" : "," + string.Join(",", listshop.Select(p => p.svalue + " as [" + p.svalue + "]").ToList())) + " from t_system WHERE typecode='000004' and svalue IN (2,4,9) ", "name,policytype,id,scmcid " + (listshop.Count == ? "" : "," + string.Join(",", listshop.Select(p => "[" + p.svalue + "]").ToList())),
"id", true, "", pageIndex, base.PageNum);
sql += ";select count(1) from t_system WHERE typecode='000004' and svalue IN (2,4,9)"; DataSet ds = code.SqlHelper.ExecuteDataset(Config.CONSQL_172_16_6_1_READ, CommandType.Text, sql); if (times == )
{
foreach (var item in listshop)
{
CreateGridColumn(item.svalue, item.name, item.svalue, , "", "width:150px;text-align:left;", gvlist);
}
times++;
}
lpolicytype = code.CacheCollection.GetSystem("", "").Where(p => p.svalue == "" || p.svalue == "" || p.svalue == "").ToList(); listslc = bll.GetModelList(); gvlist.DataSource = ds.Tables[];
gvlist.DataBind(); pager.PageSize = base.PageNum;
pager.RecordCount = Convert.ToInt32(ds.Tables[].Rows[][]);
pager.CurrentPageIndex = pageIndex;
} protected void gvlist_RowCommand(object sender, GridViewCommandEventArgs e)
{ if (e.CommandName == "udtallstate")
{
#region udt
try
{
GridViewRow r = (GridViewRow)((e.CommandSource as LinkButton).NamingContainer);
int countmodify = ;
int countadd = ;
//listshop = code.CacheCollection.GetSystem("000001", platform);
//listslc = bll.GetModelList();
string[] valueStr = e.CommandArgument.ToString().Split(',');
int scmcid = StringUtils.StrToInt(valueStr[], -);
int policytype = StringUtils.StrToInt(valueStr[], -); LinkButton lbt = ((LinkButton)r.FindControl("lbtModify"));
bool state = lbt.Text == "禁用" ? false : true;
bool tagerstate = !state; string seq = (r.RowIndex + ).ToString().PadLeft(, '');
foreach (var item in listshop)
{
int slcid = StringUtils.StrToInt(Request.Form[string.Format("gvlist$ctl{0}$hfslcid_{1}_{2}", seq, policytype, item.svalue)], -);
var list = listslc.Where(p => p.id == slcid && p.state == (state ? "Y" : "N")).ToList();
if (list.Count > )
{
countmodify += bll.UpdateByStatus(slcid, (tagerstate ? "Y" : "N")) ? : ;
string upStr = "状态" + list.First().state + "=>" + (tagerstate ? "Y" : "N");
blllog.Add(slcid.ToString(), UserOnline.Current.UserID, UserOnline.Current.UserName, upStr, "t_set_Leave_config");
}
}
if (countmodify > || countadd > )
{
base.Alert("保存成功");
//base.Alert(string.Format("更新数量:{0},新增数量:{1}", countmodify, countadd));
}
PageRender();
}
catch (Exception ex)
{
base.Alert("保存失败");
}
#endregion udt
}
else if (e.CommandName == "updateall")
{
try
{
//listshop = code.CacheCollection.GetSystem("000001", platform);
CheckBox chk;
int countmodify = ;
int countadd = ;
foreach (GridViewRow r in gvlist.Rows)
{
chk = (CheckBox)r.FindControl("cbItem");
if (chk != null)
{
if (chk.Checked)
{
int policytype = StringUtils.StrToInt(((HiddenField)r.FindControl("hfpolicytype")).Value, -);
//"gvData$ctl" + (i + 2).ToString().PadLeft(2, '0') + "$
//Request.Form["gvlist$ctl02$txtliupiaoprice_2_1"]
string seq = (r.RowIndex + ).ToString().PadLeft(, '');
foreach (var item in listshop)
{
int slcid = StringUtils.StrToInt(Request.Form[string.Format("gvlist$ctl{0}$hfslcid_{1}_{2}", seq, policytype, item.svalue)], -);
int scmcid = StringUtils.StrToInt(hfscmcid.Value, -);
if (slcid > )
{
string upStr = "";
if (ModifyByForm(ref upStr, slcid, scmcid, seq, policytype.ToString(), item.svalue))
{
countmodify++;
if (!string.IsNullOrEmpty(upStr))
blllog.Add(slcid.ToString(), UserOnline.Current.UserID, UserOnline.Current.UserName, upStr, "t_set_Leave_config");
}
}
else
{
int newid = ;
if (AddByForm(ref newid, scmcid, seq, policytype.ToString(), item.svalue))
{
blllog.Add(newid.ToString(), UserOnline.Current.UserID, UserOnline.Current.UserName, "新增配置留点留钱(id:" + newid.ToString(), "t_set_Leave_config");
countadd++;
}
}
} }
}
}
if (countmodify > || countadd > )
{
//base.Alert(string.Format("更新数量:{0},新增数量:{1}", countmodify, countadd));
ScriptHelper.PopMessageRunScript(string.Format("更新数量:{0},新增数量:{1}", countmodify, countadd), "DialogCloseAndReload();");
}
else
{
base.Alert("请选择要更新的选项");
}
}
catch (Exception ex)
{
base.Alert("保存失败");
}
} } protected void btnsearch_Click(object sender, EventArgs e)
{
pager.CurrentPageIndex = ;
PageRender();
} protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
pager.CurrentPageIndex = e.NewPageIndex;
pageIndex = pager.CurrentPageIndex;
PageRender();
} protected void gvlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (DataBinder.Eval(e.Row.DataItem, "scmcid") != null)
{
int scmcid = StringUtils.StrToInt(DataBinder.Eval(e.Row.DataItem, "scmcid").ToString(), -);
string policytype = DataBinder.Eval(e.Row.DataItem, "policytype").ToString();
var list = listslc.Where(p => p.scmcid == scmcid && p.state == "Y" && p.policytype.Value.ToString() == policytype).ToList();
LinkButton lbt = (LinkButton)e.Row.FindControl("lbtModify");
lbt.Text = (list.Count > && listshop.Count > ) ? "启用" : "禁用";
} Label lbl = new Label();
TextBox txt = new TextBox();
foreach (var item in listshop)
{
if (DataBinder.Eval(e.Row.DataItem, item.svalue) != null && DataBinder.Eval(e.Row.DataItem, "id") != null)
{
int indexcol = GetGridViewColumnIndex(gvlist, item.svalue);
string policytype = DataBinder.Eval(e.Row.DataItem, "policytype").ToString();
string shopname = DataBinder.Eval(e.Row.DataItem, item.svalue).ToString();
var list = listslc.Where(p => p.shopname == int.Parse(shopname) && p.policytype.Value.ToString() == policytype && DataBinder.Eval(e.Row.DataItem, "scmcid").ToString() == p.scmcid.ToString()).ToList(); #region 动态添加控件
//留票面 返点 返钱 cpc返点 cpc返钱 有效起始 有效结束 状态
//e.Row.Cells[indexcol].Text = "";
lbl = new Label();
lbl.ID = "lblliupiaoprice" + "_" + policytype + "_" + shopname;
lbl.Text = "留票面:";
e.Row.Cells[indexcol].Controls.Add(lbl);
txt = new TextBox();
txt.ID = "txtliupiaoprice" + "_" + policytype + "_" + shopname;
txt.Width = new Unit("70px");
txt.EnableViewState = true;
txt.Text = list.Count == ? "" : list.First().liupiaoprice.ToString();
e.Row.Cells[indexcol].Controls.Add(txt);
e.Row.Cells[indexcol].Controls.Add(new Literal() { Text = "<br/>" });
lbl = new Label();
lbl.ID = "lblliupoint" + "_" + policytype + "_" + shopname;
lbl.Text = "返点:";
e.Row.Cells[indexcol].Controls.Add(lbl);
txt = new TextBox();
txt.ID = "txtliupoint" + "_" + policytype + "_" + shopname;
txt.Width = new Unit("70px");
txt.EnableViewState = true;
txt.Text = list.Count == ? "" : list.First().liupoint.ToString();
e.Row.Cells[indexcol].Controls.Add(txt);
e.Row.Cells[indexcol].Controls.Add(new Literal() { Text = "<br/>" });
lbl = new Label();
lbl.ID = "lblliumoney" + "_" + policytype + "_" + shopname;
lbl.Text = "返钱:";
e.Row.Cells[indexcol].Controls.Add(lbl);
txt = new TextBox();
txt.ID = "txtliumoney" + "_" + policytype + "_" + shopname;
txt.Width = new Unit("70px");
txt.EnableViewState = true;
txt.Text = list.Count == ? "" : list.First().liumoney.ToString();
e.Row.Cells[indexcol].Controls.Add(txt);
e.Row.Cells[indexcol].Controls.Add(new Literal() { Text = "<br/>" });
lbl = new Label();
lbl.ID = "lblstarttime" + "_" + policytype + "_" + shopname;
lbl.Text = "有效起始:";
e.Row.Cells[indexcol].Controls.Add(lbl);
txt = new TextBox();
txt.ID = "txtstarttime" + "_" + policytype + "_" + shopname;
txt.Width = new Unit("70px");
txt.EnableViewState = true;
txt.Text = list.Count == ? "" : (string.IsNullOrEmpty(list.First().starttime.ToString()) ? "" : Convert.ToDateTime(list.First().starttime).ToString("yyyy-MM-dd"));
txt.Attributes.Add("rel", "datetime");
e.Row.Cells[indexcol].Controls.Add(txt);
e.Row.Cells[indexcol].Controls.Add(new Literal() { Text = "<br/>" });
lbl = new Label();
lbl.ID = "lblendtime" + "_" + policytype + "_" + shopname;
lbl.Text = "有效结束:";
e.Row.Cells[indexcol].Controls.Add(lbl);
txt = new TextBox();
txt.ID = "txtendtime" + "_" + policytype + "_" + shopname;
txt.Width = new Unit("70px");
txt.EnableViewState = true;
txt.Text = list.Count == ? "" : (string.IsNullOrEmpty(list.First().endtime.ToString()) ? "" : Convert.ToDateTime(list.First().endtime).ToString("yyyy-MM-dd"));
txt.Attributes.Add("rel", "datetime");
e.Row.Cells[indexcol].Controls.Add(txt);
e.Row.Cells[indexcol].Controls.Add(new Literal() { Text = "<br/>" });
lbl = new Label();
lbl.ID = "lblstate" + "_" + policytype + "_" + shopname;
lbl.Text = "状态:";
e.Row.Cells[indexcol].Controls.Add(lbl);
DropDownList ddl = new DropDownList();
ddl.ID = "ddlstate" + "_" + policytype + "_" + shopname;
ddl.Items.Add(new ListItem("启用", "Y"));
ddl.Items.Add(new ListItem("禁用", "N"));
ddl.Width = new Unit("70px");
ddl.EnableViewState = true;
ddl.SelectedValue = list.Count == ? "Y" : list.First().state;
e.Row.Cells[indexcol].Controls.Add(ddl);
e.Row.Cells[indexcol].Controls.Add(new Literal() { Text = "<br/>" });
if (platform == "")
{
lbl = new Label();
lbl.ID = "lblcpcliupoint" + "_" + policytype + "_" + shopname;
lbl.Text = "cpc返点:";
e.Row.Cells[indexcol].Controls.Add(lbl);
txt = new TextBox();
txt.ID = "txtcpcliupoint" + "_" + policytype + "_" + shopname;
txt.Width = new Unit("70px");
txt.EnableViewState = true;
txt.Text = list.Count == ? "" : list.First().cpcliupoint.ToString();
e.Row.Cells[indexcol].Controls.Add(txt);
e.Row.Cells[indexcol].Controls.Add(new Literal() { Text = "<br/>" });
lbl = new Label();
lbl.ID = "lblcpcliumoney" + "_" + policytype + "_" + shopname;
lbl.Text = "cpc返钱:";
e.Row.Cells[indexcol].Controls.Add(lbl);
txt = new TextBox();
txt.ID = "txtcpcliumoney" + "_" + policytype + "_" + shopname;
txt.Width = new Unit("70px");
txt.EnableViewState = true;
txt.Text = list.Count == ? "" : list.First().cpcliumoney.ToString();
e.Row.Cells[indexcol].Controls.Add(txt);
e.Row.Cells[indexcol].Controls.Add(new Literal() { Text = "<br/>" });
}
e.Row.Cells[indexcol].Controls.Add(new HiddenField() { Value = (list.Count == ? "-1" : list.First().id.ToString()), ID = "hfslcid" + "_" + policytype + "_" + shopname }); #endregion }
}
}
} bool AddByForm(ref int newid, int scmcid, string seq, string policytype, string shopname)
{
webframework.model.modelsetLeaveconfig model = new webframework.model.modelsetLeaveconfig(); string liupiaoprice = Request.Form[string.Format("gvlist$ctl{0}$txtliupiaoprice_{1}_{2}", seq, policytype, shopname)].Trim();
string liupoint = Request.Form[string.Format("gvlist$ctl{0}$txtliupoint_{1}_{2}", seq, policytype, shopname)].Trim();
string liumoney = Request.Form[string.Format("gvlist$ctl{0}$txtliumoney_{1}_{2}", seq, policytype, shopname)].Trim();
string starttime = Request.Form[string.Format("gvlist$ctl{0}$txtstarttime_{1}_{2}", seq, policytype, shopname)].Trim();
string endtime = Request.Form[string.Format("gvlist$ctl{0}$txtendtime_{1}_{2}", seq, policytype, shopname)].Trim();
string state = Request.Form[string.Format("gvlist$ctl{0}$ddlstate_{1}_{2}", seq, policytype,
shopname)].Trim();
model.scmcid = scmcid;
model.liupiaoprice = StringUtils.StrToDecimal(liupiaoprice, );
model.liumoney = StringUtils.StrToDecimal(liumoney, );
model.liupoint = StringUtils.StrToDecimal(liupoint, );
model.starttime = StringUtils.StrToDataTime(starttime, null);
model.endtime = StringUtils.StrToDataTime(endtime, null);
model.platform = int.Parse(hfplatform.Value);
model.shopname = int.Parse(shopname);
model.policytype = int.Parse(policytype);
model.state = state;
model.createtime = DateTime.Now;
model.createuser = UserOnline.Current.UserName;
if (platform == "")
{
string cpcliupoint = Request.Form[string.Format("gvlist$ctl{0}$txtcpcliupoint_{1}_{2}", seq, policytype, shopname)].Trim();
string cpcliumoney = Request.Form[string.Format("gvlist$ctl{0}$txtcpcliumoney_{1}_{2}", seq, policytype, shopname)].Trim();
model.cpcliumoney = StringUtils.StrToDecimal(cpcliumoney, );
model.cpcliupoint = StringUtils.StrToDecimal(cpcliupoint, );
} newid = bll.Add(model);
return newid > ? true : false;
}
bool ModifyByForm(ref string upStr, int slcid, int scmcid, string seq, string policytype, string shopname)
{
webframework.model.modelsetLeaveconfig model = bll.GetModel(slcid);
bool isupdate = false;
string liupiaoprice = Request.Form[string.Format("gvlist$ctl{0}$txtliupiaoprice_{1}_{2}", seq, policytype, shopname)].Trim();
string liupoint = Request.Form[string.Format("gvlist$ctl{0}$txtliupoint_{1}_{2}", seq, policytype, shopname)].Trim();
string liumoney = Request.Form[string.Format("gvlist$ctl{0}$txtliumoney_{1}_{2}", seq, policytype, shopname)].Trim();
string cpcliupoint = platform == "" ? Request.Form[string.Format("gvlist$ctl{0}$txtcpcliupoint_{1}_{2}", seq, policytype, shopname)].Trim() : "";
string cpcliumoney = platform == "" ? Request.Form[string.Format("gvlist$ctl{0}$txtcpcliumoney_{1}_{2}", seq, policytype, shopname)].Trim() : "";
string starttime = Request.Form[string.Format("gvlist$ctl{0}$txtstarttime_{1}_{2}", seq, policytype, shopname)].Trim();
string endtime = Request.Form[string.Format("gvlist$ctl{0}$txtendtime_{1}_{2}", seq, policytype, shopname)].Trim();
string state = Request.Form[string.Format("gvlist$ctl{0}$ddlstate_{1}_{2}", seq, policytype,
shopname)].Trim();
model.platform = int.Parse(hfplatform.Value);
model.shopname = int.Parse(shopname);
model.scmcid = scmcid; decimal dtry = ;
if (!decimal.TryParse(liupiaoprice, out dtry))
{
//base.Alert("留票面必须为小数");
return false;
}
if (!decimal.TryParse(liumoney, out dtry))
{
//base.Alert("返钱必须为小数");
return false;
}
if (!decimal.TryParse(liupoint, out dtry))
{
//base.Alert("返点必须为小数");
return false;
} if (model.state != state)
{
upStr += "[状态:" + model.state + "→" + state + "]";
model.state = state;
isupdate = true;
}
if (model.liupiaoprice != null && model.liupiaoprice.Value != decimal.Parse(liupiaoprice))
{
upStr += "[留票面:" + model.liupiaoprice + "→" + liupiaoprice + "]";
model.liupiaoprice = decimal.Parse(liupiaoprice);
isupdate = true;
}
if (model.liupiaoprice != null && model.liupiaoprice.Value != decimal.Parse(liupiaoprice))
{
upStr += "[留票面:" + model.liupiaoprice + "→" + liupiaoprice + "]";
model.liupiaoprice = decimal.Parse(liupiaoprice);
isupdate = true;
}
if (model.liumoney != null && model.liumoney.Value != decimal.Parse(liumoney))
{
upStr += "[返钱:" + model.liumoney + "→" + liumoney + "]";
model.liumoney = decimal.Parse(liumoney);
isupdate = true;
}
if (model.liupoint != null && model.liupoint.Value != decimal.Parse(liupoint))
{
upStr += "[返点:" + model.liupoint + "→" + liupoint + "]";
model.liupoint = decimal.Parse(liupoint);
isupdate = true;
} if (platform == "") //去哪儿平台才有cpc
{
if (model.cpcliumoney != null && !string.IsNullOrEmpty(cpcliumoney) && model.cpcliumoney.Value != decimal.Parse(cpcliumoney))
{
upStr += "[cpc返钱:" + model.cpcliumoney + "→" + cpcliumoney + "]";
model.cpcliumoney = decimal.Parse(cpcliumoney);
isupdate = true;
} if (model.cpcliupoint != null && !string.IsNullOrEmpty(cpcliupoint) && model.cpcliupoint.Value != decimal.Parse(cpcliupoint))
{
upStr += "[cpc返点:" + model.cpcliupoint + "→" + cpcliupoint + "]";
model.cpcliupoint = decimal.Parse(cpcliupoint);
isupdate = true;
}
} if (model.starttime == null || model.starttime.ToString() == "")
{
if (!string.IsNullOrEmpty(starttime))
{
upStr += "[有效起始日期:''→" + starttime + "]";
model.starttime = DateTime.Parse(starttime);
}
isupdate = true;
}
else if (model.starttime != null && model.starttime.ToString() != starttime)
{
upStr += "[有效起始日期:" + model.starttime.ToString() + "→" + starttime + "]";
if (!string.IsNullOrEmpty(starttime))
{
model.starttime = DateTime.Parse(starttime);
}
else
{
model.starttime = DateTime.Now.Date; ;
}
isupdate = true;
}
if (model.endtime == null || model.endtime.ToString() == "")
{
if (!string.IsNullOrEmpty(endtime))
{
upStr += "[有效结束日期:''→" + endtime + "]";
model.endtime = DateTime.Parse(endtime);
isupdate = true;
}
}
else if (model.endtime != null && model.endtime.ToString() != endtime)
{
upStr += "[有效结束日期:" + model.endtime.ToString() + "→" + endtime + "]";
if (!string.IsNullOrEmpty(endtime))
{
model.endtime = DateTime.Parse(endtime);
}
else
{
model.endtime = DateTime.Parse(DateTime.Now.ToString("yyyy-12-31"));
}
isupdate = true;
}
if (isupdate)
{
model.updatetime = DateTime.Now;
model.updateuser = UserOnline.Current.UserName;
} return bll.Update(model);
}
//创建GridView列的方法
private void CreateGridColumn(string dataField, string headerText, string footerText, int width, string headerStyle, string itemStyle, GridView gv)
{
BoundField bc = new BoundField();
bc.FooterText = footerText;
bc.DataField = dataField;
bc.HeaderText = headerText;
if (!string.IsNullOrEmpty(headerStyle))
bc.HeaderStyle.CssClass = headerStyle; //若有默认样式,此行代码及对应的参数可以移除
if (!string.IsNullOrEmpty(itemStyle))
bc.ItemStyle.CssClass = itemStyle; //若有默认样式,此行代码及对应的参数可以移除
gv.Columns.Add(bc); //把动态创建的列,添加到GridView中
if (width > )
gv.Width = new Unit(gv.Width.Value + width); //每添加一列后,要增加GridView的总体宽度 } private int GetGridViewColumnIndex(GridView grid, string colName)
{
int ndx = ;
foreach (DataControlField oCol in grid.Columns)
{
if (oCol.GetType() == typeof(BoundField))
{
BoundField bfield = (BoundField)oCol;
if (bfield.DataField.ToUpper() == colName.ToUpper())
{
return ndx;
}
}
ndx++;
}
return ndx;
} }
}

BasePage:

 using System;
using System.Collections.Generic; using System.Web; namespace web.code
{
public class BasePage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
if (!UserOnline.Current.IsLogin)
{
Response.Redirect("/login.aspx", true);
}
//base.OnInit(e);
} protected int PageNum = ; protected void Alert(string str)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('" + str + "');", true);
} }
}

注意:动态添加列通过如下获取

//"gvlist$ctl" + (i + 2).ToString().PadLeft(2, '0') + "$controlid" 
//Request.Form["gridviewID$ctl行号$控件ID"]

参考:

http://www.cnblogs.com/jiutianxingchen/p/5520844.html