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

asp.net gridview动态添加列,并获取其数据;的更多相关文章

  1. GridView动态添加列并判断绑定数据DataTable的列类型控制展示内容

    此篇随笔是2013年根据项目需求开发记录的,不一定符合大众口味,只需了解开发思路,毕竟解决方案多种多样. 下面简单说说需求点吧: (1)通过下拉列表可以选择一个DataSet(数据集),一个DataS ...

  2. ASP.NET网页动态添加、更新或删除数据行

    ASP.NET网页动态添加.更新或删除数据行 看过此篇<ASP.NET网页动态添加数据行> http://www.cnblogs.com/insus/p/3247935.html的网友,也 ...

  3. GridView动态添加列之后,导致PostBack(回发)页面数据丢失问题解决

    直入主题,首先声明,这个问题是无法解决的,特此在这说明 一.如何动态添加列,如下: 在页面重写OnInit事件,至于为什么要在这个事件写,根据页面的声明周期和经验可知(不用去别的地方找了,这个我找了之 ...

  4. gridview动态添加列的问题

    相信大家也和我一样遇到过这种问题,gridview在生成列的时候当列不确定怎么办?下面分享一下自己的解决方法. 举个列子说明一下. 普通列的添加比较简单. BoundField bf = new Bo ...

  5. ASP.NET网页动态添加数据行

    一看到这标题<ASP.NET网页动态添加数据行>,想起来似乎有点难实现.因为网页的周期性原因,往往在PostBack之后,状态难于有所保留.但Insus.NET又想实现这样的效果,用户点击 ...

  6. [转]RDLC报表——动态添加列

    本文转自:http://www.cnblogs.com/pszw/archive/2012/07/19/2599937.html 前言 最近接到一个需求:在给定的数据源中,某(些)列,可能需要单独统计 ...

  7. GridView 动态添加绑定列和模板列

    动态添加绑定列很简单:例如: GridView1.DataSourceID = "SqlDataSource1"; BoundField bf1 = new BoundField( ...

  8. DataGridview动态添加列

    1.获取数据源(select * from table名称) 2.动态绑定数据源 private void GetTableInfo(DataTable dt) { listBh = new List ...

  9. WPF GridView动态添加项并读取数据

    假设数据库有如下表, 首先我们创建一个WPF工程,界面如下 <Window x:Class="WpfApplication2.MainWindow" xmlns=" ...

随机推荐

  1. PYTHON 链接 Oracle

    一.  cx_Oracle Python 连接Oracle 数据库,需要使用cx_Oracle 包. 该包的下载地址:http://cx-Oracle.sourceforge.net/ 下载的时候,注 ...

  2. Ubuntu 64位下搭建ADT的种种问题

    我使用的adt版本为 adt-bundle-linux-x86_64-20140702.zip 1. Eclipse启动时提示 adb 无法加载动态链接库 libstdc++.so.6 以及  lib ...

  3. 【笔记】Service的使用

    一.创建Service 1.创建一个myService类,来继承Service.重写其中的方法,包括:onCreate(),onStartCommend(),onDestroy(),onBind()方 ...

  4. Spring中的IOC\DI\AOP等概念的简单学习

    IoC(Inversion of Control,控制反转).这是spring的核心,贯穿始终, 所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系.Spr ...

  5. 完整java开发中JDBC连接数据库代码和步骤

    JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.l ...

  6. SQLite数据库在多线程写锁文件的解决办法

    参考了很多SQLITE数据库多线程的解决办法 我自己写了一个SQLITEHELPER 来解决这个问题 希望大家多多指教 调用的时候  SQLLiteDBHelper _SQLLiteDBHelper ...

  7. c语言结构体&常指针和常量指针的区别

    结构体: 关系密切但数据类型不尽相同, 常指针和常量指针的区别: char * const cp : 定义一个指向字符的指针常数,即const指针,常指针. const char* p : 定义一个指 ...

  8. Neo4j批量插入(Batch Insertion)

    新建一个maven工程,这里不赘述如何新建maven工程. 添加Neo4j jar到你的工程 有两种方式: 上网站官网下载jar包,根据自己的系统下载不同的压缩包,详细过程不描述,请自行搜索其他博客 ...

  9. LeakCanary Android 和 Java 内存泄露检测。

    开始使用 在 build.gradle 中加入引用,不同的编译使用不同的引用: dependencies { debugCompile 'com.squareup.leakcanary:leakcan ...

  10. git提交远程仓库命令

    在已有的git库中搭建新库,并且将本地的git仓库,上传到远程服务器的git库中,从而开始一个新的项目 首先,在本地新建文件夹abc,进入到abc里面,然后git init.这样就在本地初始化了一个g ...