列字段计算意思是把明细表数值类型字段合计后赋值给主表数值类字段。 AddFormColCal.aspx html xmlns="http://www.w3.org/1999/xhtml"head runat="server" title列字段规则/title/headbody form id="form1" runat="serv
列字段计算意思是把明细表数值类型字段合计后赋值给主表数值类字段。
AddFormColCal.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>列字段规则</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="table_bgcolor">
<tr>
<td style="width: 100%; text-align: left;" class="table_titlebgcolor">
表单管理:列字段规则
</td>
</tr>
<tr>
<td style="width: 100%; text-align: left;">
表单名称:<asp:Label runat="server" ID="FormNameLabel"></asp:Label>
</td>
</tr>
<tr>
<td style="width: 100%; text-align: left;">
表单描述:<asp:Label runat="server" ID="FormDesLabel"></asp:Label>
</td>
</tr>
<tr>
<td style="width: 100%; text-align: left;">
明细字段规则
</td>
</tr>
<tr>
<td style="width: 100%; text-align: left;">
<asp:GridView runat="server" ID="AddFormColCalGV" DataKeyNames="id" AutoGenerateColumns="false" EmptyDataText="没有可用明细字段!"
SkinID="GridViewSkin" onrowdatabound="AddFormColCalGV_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="是否合计">
<ItemTemplate>
<asp:CheckBox runat="server" ID="SumCheckBox" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="明细字段">
<ItemTemplate>
<asp:Label runat="server" ID="DetailLabel" Text='<%#Eval("fieldlabel") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="赋值给主字段">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ValueToMainFieldDDL" DataTextField="fieldname" DataValueField="id">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="SaveButton" Text="保存" OnClick="onSaveButtonClick" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后端代码
AddFormColCal.aspx.cs
#region EventHandler
/// <summary>
/// 保存列字段规则
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void onSaveButtonClick(object sender, EventArgs e)
{
string colcalstr = "";
string maincalstr = "";
foreach (GridViewRow row in AddFormColCalGV.Rows)
{
int fieldid = (int)AddFormColCalGV.DataKeys[row.RowIndex]["id"];
CheckBox SumCheckBox = row.FindControl("SumCheckBox") as CheckBox;
DropDownList ddl = row.FindControl("ValueToMainFieldDDL") as DropDownList;
if (SumCheckBox.Checked)
{//合计 colcalstr
colcalstr += ";detailfield_" + fieldid;
}
if (colcalstr.StartsWith(";"))
{
colcalstr = colcalstr.Remove(0, 1);
}
if (ddl.SelectedIndex > 0)
{//赋值给主字段 maincalstr
maincalstr += ";mainfield_" + ddl.SelectedValue + "=detailfield_" + fieldid;
}
if (maincalstr.StartsWith(";"))
{
maincalstr = maincalstr.Remove(0, 1);
}
}
int formid = int.Parse(ViewState["formid"].ToString());
M.workflow_formdetailinfo formdetailinfoModel = formdetailinfoBLL.GetModel(formid);
if (formdetailinfoModel != null)
{
if (string.IsNullOrEmpty(colcalstr) &&
string.IsNullOrEmpty(maincalstr) &&
string.IsNullOrEmpty(formdetailinfoModel.rowcalstr))
{
formdetailinfoBLL.Delete(formid);
}
else
{
formdetailinfoModel.colcalstr = colcalstr;
formdetailinfoModel.maincalstr = maincalstr;
formdetailinfoBLL.Update(formdetailinfoModel);
}
}
else
{
formdetailinfoModel = new M.workflow_formdetailinfo();
formdetailinfoModel.colcalstr = colcalstr;
formdetailinfoModel.maincalstr = maincalstr;
formdetailinfoBLL.Add(formdetailinfoModel);
}
bindColCalRule(formid.ToString());
}
protected void AddFormColCalGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("ValueToMainFieldDDL") as DropDownList;
if (ddl != null)
{
List<M.workflow_billfield> mainFieldList = billfieldBLL.GetModelList("viewtype=0 and fieldhtmltype=1 and (type=2 or type=3) and billid=" + ViewState["formid"].ToString());
ddl.DataSource = mainFieldList;
ddl.DataBind();
if (mainFieldList != null && mainFieldList.Count > 0)
{
ddl.Items.Insert(0, new ListItem("", "-1"));
}
}
CheckBox SumCheckBox = e.Row.FindControl("SumCheckBox") as CheckBox;
int fieldid = (int)AddFormColCalGV.DataKeys[e.Row.RowIndex]["id"];
M.workflow_formdetailinfo formdetailinfoModel = formdetailinfoBLL.GetModel(int.Parse(ViewState["formid"].ToString()));
if (formdetailinfoModel != null)
{
string colcalstr = formdetailinfoModel.colcalstr;
SumCheckBox.Checked = colcalstr.IndexOf("detailfield_" + fieldid) > -1;
string maincalstr = formdetailinfoModel.maincalstr;
foreach (ListItem item in ddl.Items)
{
item.Selected = maincalstr.IndexOf("mainfield_" + item.Value + "=detailfield_" + fieldid) > -1;
}
}
}
}
#endregion //End EventHandler
#region Helper Methods
private void initialForm()
{
string formid = ViewState["formid"].ToString();
int id = -1;
if (int.TryParse(formid, out id))
{
M.workflow_bill form = formBLL.GetModel(id);
if (form != null)
{
FormNameLabel.Text = form.namelabel;
FormDesLabel.Text = form.formdes;
}
}
bindColCalRule(formid);
}
private void bindColCalRule(string formid)
{
List<M.workflow_billfield> detailFieldList = billfieldBLL.GetModelList("viewtype=1 and fieldhtmltype=1 and (type=2 or type=3) and billid=" + formid + " order by detailtable, dsporder, id");
AddFormColCalGV.DataSource = detailFieldList;
AddFormColCalGV.DataBind();
}
#endregion //End Helper Methods
#region Fields
private BLL.WorkFlow.workflow_bill formBLL = new B.workflow_bill();
private B.workflow_formdetailinfo formdetailinfoBLL = new B.workflow_formdetailinfo();
private B.workflow_billfield billfieldBLL = new B.workflow_billfield();
#endregion //End Fields