protected void grvList_RowUpdating(object sender, GridViewUpdateEventArgs e) { HtmlComboBox hcbUNIT = (HtmlComboBox)grvList.Rows[e.RowIndex].FindControl("hcbUNIT"); TextBox txtXH = (TextBox)grvList.Rows[e.RowIndex].FindControl("txtXH"); int temp; if (!int.TryParse(txtXH.Text, out temp)) { detail_info.InnerText = GetGlobalResourceObject("WebGlobalResource", "NumericalValeError").ToString();; return; } TextBox txtCONTENT = (TextBox)grvList.Rows[e.RowIndex].FindControl("txtCONTENT"); FieldPara[] fields = new FieldPara[3] { new FieldPara("UNIT", FieldType.String, hcbUNIT.SelectedText), new FieldPara("CONTENT", FieldType.String, txtCONTENT.Text), new FieldPara("XH", FieldType.Int, txtXH.Text) }; WherePara[] wheres = new WherePara[1] { new WherePara("TID", FieldType.Int, grvList.DataKeys[e.RowIndex].Value.ToString(), "=", "and") }; _sql = DBOpt.dbHelper.GetUpdateSql("T_DD_TYPICAL_OPT_BODY", fields, wheres); if (DBOpt.dbHelper.ExecuteSql(_sql) > 0) { grvList.EditIndex = -1; initGridView(); detail_info.InnerText = GetGlobalResourceObject("WebGlobalResource", "SaveSuccessMessage").ToString(); } else { detail_info.InnerText = GetGlobalResourceObject("WebGlobalResource", "SaveFailMessage").ToString(); return; } }
/// <summary> /// 在GridView控件的RowUpdating事件中调用,用于获取更新的SQL语句 /// </summary> /// <param name="grv"></param> /// <param name="TableID"></param> /// <param name="EditIndex"></param> /// <param name="TIDValue"></param> /// <returns></returns> public static string GetGridViewRowUpdating(ref GridView grv, int TableID, int EditIndex, int TIDValue) { string tableName, sql; string contolType; tableName = DBOpt.dbHelper.ExecuteScalar("select NAME from DMIS_SYS_TABLES where ID=" + TableID.ToString()).ToString(); //共有多少列数据要保存,CUSTOM_CONTROL_NAME列一定是配置了控件名的,如果不要保存的列则定要删除控件名 sql = "select NAME,DESCR,TYPE,CUSTOM_CONTROL_NAME,CUSTOM_CONTROL_TYPE,CUSTOM_CONTROL_SVAE_TYPE from DMIS_SYS_COLUMNS where TABLE_ID=" + TableID.ToString() + " and CUSTOM_CONTROL_NAME is not NULL order by ORDER_ID"; DataTable dt = DBOpt.dbHelper.GetDataTable(sql); FieldPara[] fields = new FieldPara[dt.Rows.Count]; TextBox txt; DropDownList ddl; CheckBox ckb; RadioButtonList rbl; WebDate wdl; WebComboBox wbx; HtmlComboBox hbx; WebDropDownList wdd; HtmlInputText hit; for (int i = 0; i < dt.Rows.Count; i++) { //没有定义控件名,则不处理保存代码 if (dt.Rows[i]["CUSTOM_CONTROL_NAME"] == Convert.DBNull || dt.Rows[i]["CUSTOM_CONTROL_NAME"].ToString().Trim() == "") { continue; } //这种方式的后果是:如果某个控件在平台设置错误,与实际没对应,则此列不会得到修改 //如果一个表有多个GridView修改,则可以减少平台中同样一个表的个数。 object obj; obj = grv.Rows[EditIndex].FindControl(dt.Rows[i]["CUSTOM_CONTROL_NAME"].ToString()); if (obj == null) { continue; } FieldPara fp = new FieldPara(); fp.fieldName = dt.Rows[i]["NAME"].ToString(); if (dt.Rows[i]["TYPE"].ToString() == "String") { fp.fieldType = FieldType.String; } else if (dt.Rows[i]["TYPE"].ToString() == "Datetime") { fp.fieldType = FieldType.Datetime; } else { fp.fieldType = FieldType.Int; } contolType = dt.Rows[i]["CUSTOM_CONTROL_TYPE"].ToString(); switch (contolType) { case "TextBox": txt = (TextBox)obj; if (txt == null) { return(i.ToString()); } fp.fieldValue = ValueToField.StringToField(txt.Text); break; case "DropDownList": ddl = (DropDownList)obj; if (ddl == null) { return(i.ToString()); } if (dt.Rows[i]["CUSTOM_CONTROL_SVAE_TYPE"].ToString() == "Text") { fp.fieldValue = ValueToField.DropDownListToFieldByText(ddl); } else { fp.fieldValue = ValueToField.DropDownListToFieldByValue(ddl); } break; case "CheckBox": ckb = (CheckBox)obj; if (ckb == null) { return(i.ToString()); } fp.fieldValue = ValueToField.CheckBoxToField(ckb.Checked); break; case "RadioButtonList": rbl = (RadioButtonList)obj; if (rbl == null) { return(i.ToString()); } if (dt.Rows[i]["CUSTOM_CONTROL_SVAE_TYPE"].ToString() == "Text") { fp.fieldValue = ValueToField.RadioButtonListToFieldByText(rbl); } else { fp.fieldValue = ValueToField.RadioButtonListToFieldByValue(rbl); } break; case "WebDateLib": wdl = (WebDate)obj; if (wdl == null) { return(i.ToString()); } fp.fieldValue = ValueToField.StringToField(wdl.Text); break; case "WebComboBox": wbx = (WebComboBox)obj; if (wbx == null) { return(i.ToString()); } fp.fieldValue = ValueToField.StringToField(wbx.Text); break; case "HtmlComboBox": hbx = (HtmlComboBox)obj; if (hbx == null) { return(i.ToString()); } fp.fieldValue = ValueToField.StringToField(hbx.SelectedText); break; case "WebDropDown": wdd = (WebDropDownList)obj; if (wdd == null) { return(i.ToString()); } fp.fieldValue = ValueToField.StringToField(wdd.Text); break; case "HtmlInputText": hit = (HtmlInputText)obj; if (hit == null) { return(i.ToString()); } fp.fieldValue = ValueToField.StringToField(hit.Value); break; default: break; } fields[i] = fp; } //主键必须是TID WherePara[] wheres = new WherePara[1] { new WherePara("TID", FieldType.Int, grv.DataKeys[EditIndex].Value.ToString(), "=", "and") }; sql = DBOpt.dbHelper.GetUpdateSql(tableName, fields, wheres); return(sql); }