// This is the Insert method to insert the entered DevInfo item // USAGE: <asp:FormView InsertMethod="InsertItem"> public void InsertItem() { _devInfo = new MongoHelper <DevInfo>(); var item = new IMserver.Models.DevInfo(); item.Type = (MyFormView.FindControl("MyDevType") as DropDownList).SelectedValue; item.CompName = (MyFormView.FindControl("MyCompName") as DropDownList).SelectedValue; TryUpdateModel(item); Expression <Func <DevInfo, bool> > ex = p => p.DevID == item.DevID; if (_devInfo.FindBy(ex).Count() > 0) { Response.Write("<script>alert('已存在拥有该设备号的设备!无法添加!');</script>"); return; } else { //取得系统时间作为设备添加时间。但是mongo默认为UTC时间会减去8小时,后取出显示时出错,所以在这加8小时 item.AddTime = DateTime.Now.AddHours(8); if (ModelState.IsValid) { try { _devInfo.Insert(item); } catch (NullReferenceException dbEx) { Response.Write("<script>alert(" + dbEx.Message + ");</script>"); } //_db.DevInfo_view.Add(item); //_db.SaveChanges(); Response.Redirect("Default"); } } }
// This is the Update methd to update the selected DevInfo_view item // USAGE: <asp:FormView UpdateMethod="UpdateItem"> public void UpdateItem(string DevID, string CompName) { ex = p => p.DevID == DevID; var item = _devInfo.FindOneBy(ex); if (item == null) { // The item wasn't found //ModelState.AddModelError("", String.Format("Item with id {0} was not found", DevID)); ModelState.AddModelError("", "找不到该设备"); return; } item.Type = (MyFormView.FindControl("MyDevType") as DropDownList).SelectedValue; item.CompName = (MyFormView.FindControl("MyCompName") as DropDownList).SelectedValue; TryUpdateModel(item); if (ModelState.IsValid) { _devInfo.Update(item); } Response.Redirect("../Default"); }
public override string GetDesignTimeHtml() { // Make the full extent of the control more visible in the designer. // If the border style is None or NotSet, change the border to // a wide, blue, dashed line. Include the caption within the border. MyFormView myGV = (MyFormView)Component; string markup = null; int charX; // Check if the border style should be changed. if (myGV.BorderStyle == BorderStyle.NotSet || myGV.BorderStyle == BorderStyle.None) { BorderStyle oldBorderStyle = myGV.BorderStyle; Unit oldBorderWidth = myGV.BorderWidth; Color oldBorderColor = myGV.BorderColor; // Set the design-time properties and catch any exceptions. try { myGV.BorderStyle = BorderStyle.Dashed; myGV.BorderWidth = Unit.Pixel(3); myGV.BorderColor = Color.Blue; // Call the base method to generate the markup. markup = base.GetDesignTimeHtml(); } catch (Exception ex) { markup = GetErrorDesignTimeHtml(ex); } finally { // Restore the properties to their original settings. myGV.BorderStyle = oldBorderStyle; myGV.BorderWidth = oldBorderWidth; myGV.BorderColor = oldBorderColor; } } else { // Call the base method to generate the markup. markup = base.GetDesignTimeHtml(); } // Look for a <caption> tag. if ((charX = markup.IndexOf(capTag)) > 0) { // Replace the first caption with // "tr><td colspan=9 align=center". // It is okay if the colspan exceeds the // number of columns in the table. markup = markup.Remove(charX, capTag.Length).Insert(charX, trOpen); // Replace the second caption with "td></tr". if ((charX = markup.IndexOf(capTag, charX)) > 0) { markup = markup.Remove(charX, capTag.Length).Insert(charX, trClose); } } return(markup); } // GetDesignTimeHtml
protected void Page_Load(object sender, EventArgs e) { (MyFormView.FindControl("MyDevType") as DropDownList).SelectedValue = ViewState["Type"].ToString(); (MyFormView.FindControl("MyCompName") as DropDownList).SelectedValue = ViewState["Comp"].ToString(); }