repeating tables for SharePoint 2010 forms
In general, there are three ways SharePoint forms
1. A specific list of InfoPath forms (you can customize the list directly in the list, only for a specific list)
2. Complex forms, such as the need for repeating tables, and the data of the repeating table to be used to statistics
3. InfoPath: Create a new InfoPath, and then, by publishing, activating, adapting to multiple lists.
The following is a development instance of a repeating table, as shown in:
The following are common code for developing this form:
Front 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= "leave type" > < 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= ' ser Ver "> <asp:ListItem> sick leave </asp:ListItem> <asp:ListItem> leave </asp:ListItem> <asp:ListItem> annual leave</asp:ListItem> <asp:ListItem> last year's annual leave </asp:ListItem> <asp:ListItem> Holiday </asp:ListItem> <asp:ListItem> last year's holiday lt;/asp:listitem> <asp:ListItem> Marriage Leave </asp:ListItem> <asp:ListItem> Bereavement </asp:ListItem> <asp:ListItem> Maternity </asp :listitem> <asp:ListItem> Paternity Leave </asp:ListItem> </asp:DropDownList> </ItemTemplate> <itemstyle Hor izontalalign= "center"/> The backend code that will be used:
Pageload The most important business logic
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")) {//This is the view, iframe mode txtbumen.enabled = false; txtzhiwei.enabled = false; Btnadd.visible = false; Btnclose.visible = false; Btnsave.visible = false; Response.Write ("false"); } if (string. IsNullOrEmpty (SID)) {//New mode viewstate["Title"] = Getsequencecode ("QJ"); Response.Write (viewstate["Title"]. ToString ()); This.txtXingmin.Text = SPContext.Current.Web.CurrentUser.Name; To go from the people Information list to the current user data SPListItem item = Getsingleitemfrompeopleinfo (SPContext.Current.Web.CurrentUser.LoginName) ; if (item! = NULL) {Txtbumen.text = Convertobject (item["category/department"]); Txtzhiwei.text = Convertobject (item["position"]); } loaddata (); } else {//edit mode viewstate["ID"] = SID; Read Data bindexistngdata (SID); } loadcalculatedata (); } }
Pageload first load is bound to bind a row of data, when the new field is true, indicates that it is a fresh data, if it is Fasle, when clicked delete the list of data will be deleted
Initialize the datatable structure 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; } New data initialization data, strap GridView row empty data 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 (); }
Traversing the GridView row to generate a DataTable
Traverse the GridView data filled in 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 features that can be removed
Delete GridView selected row data 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) { //is not new data, the data of the leave single table is also deleted 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 Row Data binding code
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e) {datatable dt = (DataTable) Vie wstate["_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.todateti Me (drs[0]["BeginTime"). ToString ()); } if (!string. IsNullOrEmpty (Convertobject (drs[0]["EndTime"])) {dtendtime.selecteddate = Convert.todatetime (d rs[0]["EndTime"]. ToString ()); } if (!string. IsNullOrEmpty (Convertobject (drs[0]["Type"))) {Ddlqjlx.selectedvalue = Convertobject (drs[0]["Ty PE "]); } } }
Save the Button code:
SPWeb web = SPContext.Current.Web; SPList mainlist = web. Lists["Leave single Main Table"]; SPList cilist = web. lists["Leave single-time table"]; Web. Allowunsafeupdates = true; String smainitemid = String. Empty; if (viewstate["id"] = NULL) {//old data, to modify string SID = viewstate["id"]. ToString (); SPListItem item = mainlist. GetItemByID (Convert.ToInt32 (SID)); item["name"] = TxtXingmin.Text.Trim (); item["department"] = TxtBumen.Text.Trim (); item["position"] = 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["name"] = TxtXingmin.Text.Trim (); mainnewitem["department"] = TxtBumen.Text.Trim (); mainnewitem["position"] = 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["Leave order type"] = Convertobject (rdr["type"]); onewitem["Start Time" = Convert.todatetime (rdr["BeginTime"]. ToString ()). ToString ("Yyyy-mm-dd"); onewitem["End Time" = Convert.todatetime (rdr["EndTIme"]. ToString ()). ToString ("Yyyy-mm-dd"); Onewitem["Remarks"] = Convertobject (rdr["Demo"]); onewitem["ParentID"] = Smainitemid; Onewitem.update (); } else {SPListItem Ocurrentitem = cilist. GetItemByID (Convert.ToInt32 (rdr["ID"). ToString ())); ocurrentitem["Leave order type"] = Convertobject (rdr["type"]); ocurrentitem["Start Time" = Convert.todatetime (rdr["BeginTime"]. ToString ()). ToString ("Yyyy-mm-dd"); ocurrentitem["End Time" = Convert.todatetime (rdr["EndTIme"]. ToString ()). ToString ("Yyyy-mm-dd"); Ocurrentitem["Remarks"] = Convertobject (rdr["Demo"]); Ocurrentitem.systemupdate (); } } }
repeating tables for SharePoint 2010 electronic Forms