一般来讲SharePoint的表单有三种方式

1. Infopath表单的对应特定的列表(直接在列表中可以自定义列表,只适用特定列表)

2. 复杂的表单,如需要重复表,且重复表的数据需用来统计

3. Infopath: 新建一个Infopath,然后通过发布,激活,适应多个列表。

下面是一个重复表的开发实例,如下图所示:

下面是开发这种表单的常用代码:

前台代码:

View Code
          <div style="padding-top: 10px;">

                <asp:GridView ID="GridView1" AutoGenerateColumns="False" HeaderStyle-CssClass="GridHeader"
                    runat="server" RowStyle-CssClass="GridRow" Width="100%" onrowdatabound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="请假类型">
                            <ItemTemplate>
                                <asp:Label ID="lbID" runat="server" Visible="false"  Text='<%# Eval("ID")%>'></asp:Label>
                                  <asp:CheckBox ID="CheckBox1" Visible="false" runat="server" Checked='<%#Eval("New") %>'/>
                                <asp:DropDownList ID="ddlQjlx" runat="server">
                                    <asp:ListItem>病假</asp:ListItem>
                                    <asp:ListItem>事假</asp:ListItem>
                                    <asp:ListItem>年假</asp:ListItem>
                                    <asp:ListItem>去年年假</asp:ListItem>
                                    <asp:ListItem>调休假</asp:ListItem>
                                    <asp:ListItem>去年调休假</asp:ListItem>
                                    <asp:ListItem>婚假</asp:ListItem>
                                    <asp:ListItem>丧假</asp:ListItem>
                                    <asp:ListItem>产假</asp:ListItem>
                                    <asp:ListItem>陪产假</asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" />
                            <HeaderStyle HorizontalAlign="Center"/>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="开始时间">
                            <ItemTemplate>
                                <SharePoint:DateTimeControl ShowWeekNumber="true" HoursMode24="true" DateOnly="true"
                                    ID="dtStartTime" runat="server"></SharePoint:DateTimeControl>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" Width="120" />
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="结束时间">
                            <ItemTemplate>
                                <SharePoint:DateTimeControl ShowWeekNumber="true" HoursMode24="true" DateOnly="true"
                                    ID="dtEndTime" runat="server"></SharePoint:DateTimeControl>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" Width="120" />
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="备注">
                            <ItemTemplate>
                                <asp:TextBox ID="txtDemo" runat="server" Width="100%" Text='<%#Eval("Demo") %>'></asp:TextBox>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Left" />
                            <HeaderStyle HorizontalAlign="Center"   Width="100%"/>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="">
                            <ItemTemplate>
                                <asp:Button ID="btnDelete" Text="删除" CssClass="btn" runat="server" OnClientClick="return confirm('是否要删除?');"
                                    OnClick="btnDelete_Click"  />
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" />
                            <HeaderStyle HorizontalAlign="Center" Width="100" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
                    <div style="padding-bottom: 6px; text-align:left; display:block; width:100%; padding-top:6px;">
            <asp:Button ID="btnAdd" runat="server" Text="增加" CssClass="btn1" OnClick="btnAdd_Click"/></div>

 

会用到的后台代码:

PageLoad最重要的业务逻辑

View Code
protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            string sID = Request["ID"];
            if (!string.IsNullOrEmpty(Request["action"]) && Request["action"].ToString().Equals("select"))
            {
                //这是视图,IFrame模式
                txtBumen.Enabled = false;
                txtZhiwei.Enabled = false;
                btnAdd.Visible = false;
                btnClose.Visible = false;
                btnSave.Visible = false;
                //Response.Write("false");
            }
            if (string.IsNullOrEmpty(sID))
            {
                //新建模式
                ViewState["Title"] = GetSequenceCode("QJ");
                //Response.Write(ViewState["Title"].ToString());
                this.txtXingmin.Text = SPContext.Current.Web.CurrentUser.Name;
                //从人员信息列表去当前用户数据
                SPListItem item = GetSingleItemFromPeopleInfo(SPContext.Current.Web.CurrentUser.LoginName);
                if (item != null)
                {
                    txtBumen.Text = ConvertObject(item["类别/部门"]);
                    txtZhiwei.Text = ConvertObject(item["职位"]);
                }
                LoadData();
            }
            else
            {
                //编辑模式
                ViewState["ID"] = sID;
                //读取数据
                BindExistngData(sID);
            }

            LoadCalculateData();
        }
    }

PageLoad首次加载需绑定一行数据,当New字段为True时,表示其为新的数据,如果为fasle时,当点击删除的时候需把列表的数据也一齐删掉

View Code
    //初始化DataTable结构
    private DataTable InitializeDataTableStructure()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(string));
        dt.Columns.Add("Type", typeof(string));
        dt.Columns.Add("BeginTime", typeof(DateTime));
        dt.Columns.Add("EndTime", typeof(DateTime));
        dt.Columns.Add("Demo", typeof(string));
        dt.Columns.Add("New", typeof(bool));
        return dt;
    }

    //新建数据初始化数据,绑带GridView一行空的数据
    private void LoadData()
    {
        DataTable dt = InitializeDataTableStructure();
        DataRow dr = dt.NewRow();
        dr["ID"] = 1;
        dr["New"] = true;
        dt.Rows.Add(dr);
        ViewState["_DT"] = dt;
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }

遍历GridView行生成DataTable

View Code
    //遍历GridView填写的数据
    protected DataTable GetDataSource()
    {
        DataTable dt = ((DataTable)ViewState["_DT"]).Clone();
        GridViewRowCollection Rows = this.GridView1.Rows;
        if (Rows != null && Rows.Count > 0)
        {
            foreach (GridViewRow gvr in Rows)
            {
                Label lbID = gvr.FindControl("lbID") as Label;
                CheckBox cbNew = gvr.FindControl("CheckBox1") as CheckBox;
                DropDownList ddlQjlx = gvr.FindControl("ddlQjlx") as DropDownList;
                DateTimeControl dtBeginTime = gvr.FindControl("dtStartTime") as DateTimeControl;
                DateTimeControl dtEndTIme = gvr.FindControl("dtEndTime") as DateTimeControl;
                TextBox txtDemo = gvr.FindControl("txtDemo") as TextBox;
                DataRow dr = dt.NewRow();
                dr["ID"] = lbID.Text;
                dr["New"] = cbNew.Checked;
                dr["Type"] = ddlQjlx.SelectedValue;
                if (!dtBeginTime.IsDateEmpty)
                {
                    dr["BeginTime"] = dtBeginTime.SelectedDate;
                }
                if (!dtEndTIme.IsDateEmpty)
                {
                    dr["EndTime"] = dtEndTIme.SelectedDate;
                }
                dr["Demo"] = txtDemo.Text;
                dt.Rows.Add(dr);
            }
        }
        ViewState["_DT"] = dt;
        return dt;
    }

GridView可删除的功能

View Code
    //删除GridView选中行的数据
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Label LabelID = ((Label)((sender as Button).Parent.Parent as GridViewRow).FindControl("lbID"));
        DataTable dt = GetDataSource();
        DataRow[] drs = dt.Select("ID='" + LabelID.Text + "'");
        bool bIsNew = Convert.ToBoolean(drs[0]["New"].ToString());
        dt.Rows.Remove(drs[0]);
        if (!bIsNew)
        {
            //不是新的数据,把请假单次表的数据也要删掉
            DeleteItemFromCiBiaoByID(LabelID.Text);
        }
        if (dt == null || dt.Rows.Count == 0)
        {
            LoadData();
            dt = (DataTable)ViewState["_DT"];
        }
        ViewState["_DT"] = dt;
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }

GridView行数据绑定代码

View Code
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataTable dt = (DataTable)ViewState["_DT"];
        //Response.Write(bv.ToString());
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            Label lbID = (Label)e.Row.FindControl("lbID");
            DropDownList ddlQjlx = e.Row.FindControl("ddlQjlx") as DropDownList;
            ddlQjlx.Enabled = btnSave.Visible;
            DateTimeControl dtBeginTime = e.Row.FindControl("dtStartTime") as DateTimeControl;
            dtBeginTime.Enabled = btnSave.Visible;
            DateTimeControl dtEndTime = e.Row.FindControl("dtEndTime") as DateTimeControl;
            dtEndTime.Enabled = btnSave.Visible;
            TextBox txtDemo = e.Row.FindControl("txtDemo") as TextBox;
            txtDemo.Enabled = btnSave.Visible;
            Button btnDelete = e.Row.FindControl("btnDelete") as Button;
            btnDelete.Visible = btnSave.Visible;
            //btnDelete.Visible = bv;
            DataRow[] drs = dt.Select("ID='" + lbID.Text + "'");
            if (!string.IsNullOrEmpty(ConvertObject(drs[0]["BeginTime"])))
            {
                dtBeginTime.SelectedDate = Convert.ToDateTime(drs[0]["BeginTime"].ToString());
            }
            if (!string.IsNullOrEmpty(ConvertObject(drs[0]["EndTime"])))
            {
                dtEndTime.SelectedDate = Convert.ToDateTime(drs[0]["EndTime"].ToString());
            }

            if (!string.IsNullOrEmpty(ConvertObject(drs[0]["Type"])))
            {
                ddlQjlx.SelectedValue = ConvertObject(drs[0]["Type"]);
            }
        }
    }

 保存按钮代码:

View Code
        SPWeb web = SPContext.Current.Web;
        SPList mainlist = web.Lists["请假单主表"];
        SPList cilist = web.Lists["请假单次表"];

        web.AllowUnsafeUpdates = true;
        string sMainItemID = string.Empty;

        if (ViewState["ID"] != null)
        {
            //旧的数据,要修改
            string sID = ViewState["ID"].ToString();
            SPListItem item = mainlist.GetItemById(Convert.ToInt32(sID));
            item["姓名"] = txtXingmin.Text.Trim();
            item["部门"] = txtBumen.Text.Trim();
            item["职位"] = txtZhiwei.Text.Trim();

            item.SystemUpdate();
            sMainItemID = item.ID.ToString();
        }
        else
        {
            SPListItem mainNewItem = mainlist.Items.Add();
            mainNewItem["Title"] = ViewState["Title"].ToString();
            mainNewItem["LoginName"] = SPContext.Current.Web.CurrentUser.LoginName;
            mainNewItem["姓名"] = txtXingmin.Text.Trim();
            mainNewItem["部门"] = txtBumen.Text.Trim();
            mainNewItem["职位"] = txtZhiwei.Text.Trim();
            mainNewItem.Update();
            sMainItemID = mainNewItem.ID.ToString();
        }
        if (dt != null & dt.Rows.Count > 0)
        {
            foreach (DataRow rdr in dt.Rows)
            {
                if (Convert.ToBoolean(rdr["New"].ToString()))
                {
                    SPListItem oNewItem = cilist.Items.Add();
                    oNewItem["请假单类型"] = ConvertObject(rdr["Type"]);
                    oNewItem["开始时间"] = Convert.ToDateTime(rdr["BeginTime"].ToString()).ToString("yyyy-MM-dd");
                    oNewItem["结束时间"] = Convert.ToDateTime(rdr["EndTIme"].ToString()).ToString("yyyy-MM-dd");
                    oNewItem["备注"] = ConvertObject(rdr["Demo"]);
                    oNewItem["ParentID"] = sMainItemID;
                    oNewItem.Update();
                }
                else
                {
                    SPListItem oCurrentItem = cilist.GetItemById(Convert.ToInt32(rdr["ID"].ToString()));
                    oCurrentItem["请假单类型"] = ConvertObject(rdr["Type"]);
                    oCurrentItem["开始时间"] = Convert.ToDateTime(rdr["BeginTime"].ToString()).ToString("yyyy-MM-dd");
                    oCurrentItem["结束时间"] = Convert.ToDateTime(rdr["EndTIme"].ToString()).ToString("yyyy-MM-dd");
                    oCurrentItem["备注"] = ConvertObject(rdr["Demo"]);
                    oCurrentItem.SystemUpdate();
                }
            }
        }

 

posted on 2012-05-17 16:45  gzh4455  阅读(866)  评论(0编辑  收藏  举报