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. 内存对齐 和 sizeof小结

    数据对齐(内存对齐)指该数据所在的地址必须是该数据长度的整数倍.X86CPU能直接访问对齐的数据,当它试图访问未对齐的数据时,会在内部进行一系列的调整,降低运行速度.数据对齐一般出现在结构体和类中,在 ...

  2. 学习linux/unix编程方法的建议(转)

    假设你是计算机科班出身,计算机系的基本课程如数据结构.操作系统.体系结构.编译原理.计算机网络你全修过 我想大概可以分为4个阶段,水平从低到高从安装使用=>linux常用命令=>linux ...

  3. 关于 jsp 解析特殊字符的问题

    在项目中了 使用了一个UI封装好 的插件 经测试了可以返回一些特殊字符,但是因为是特殊字符,导致了jsp解析出错,使用了Jquery来添加了dom结构,添加完之后,Ui控件进行初始化的时候报错了,原因 ...

  4. hdu1000,hdu1001,hdu1002,hdu1003

    hdu1000 仅仅是为了纪念 #include <cstdio> int main() { int a,b; while (scanf("%d%d",&a,& ...

  5. PHP中FOREACH()用法

    PHP 4 引入了 foreach 结构,和 Perl 以及其他语言很像.这只是一种遍历数组简便方法.foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误. 1. ...

  6. mtk开发

    mtk套接字所有的声明放在soc_api.h 条件编译命令最常见的形式为: ? 1 2 3 4 5 #ifdef标识符 //程序段1 #else //程序段2 #endif 它的作用是:当标识符已经被 ...

  7. 利用 iframe解决ajax的跨域问题

    问题 1. form提交或a标签跳转方式提交不会引发跨域问题. 2. ajax出于安全问题就有了跨域问题,因为一次请求中既访问了外部域最后返回了自己的域. 3. 用iframe其实就是想仿照ajax的 ...

  8. cookie httponly属性

    Marks the cookie as accessible only through the HTTP protocol. This means that the cookie won't be a ...

  9. JDBC连接数据库(PreparedStatement)

    PreparedStatement是在数据库端防止SQL注入漏洞的SQL方法这里演示了一些基本使用方法同样使用Oracle数据库,之前已经手动建立了一张t_account表数据库代码参见上一篇< ...

  10. ubuntu14.04 下emacs 24 配置

    目的: 配置emacs 24 适合编程开发 主要参考JerryZhang的配置(Emacs 简易教程) http://www.perfect-is-shit.com/emacs-simple-tuto ...