Intro
廢話少說XD,網頁就是要不斷 Add/Edit/View...
明明同一個Layout為何要做一個編輯, 一個檢視咧.
基於這個想法, 就試著做一個切換方式吧!!
重點在 override Render
Summary
1. 建一個interface IViewEditControl 讓控制項有共同 function
2. 繼承原生控制項, 實作咱們的interface, 並覆寫Render
3. 切換模式吧!!
<asp:GridView ID="gvList" runat="server" OnRowEditing="gvList_RowEditing"> <Columns><asp:TemplateField> <ItemTemplate> <asp:Button ID="btnEdit" runat="server" CommandName="Edit" /> </ItemTemplate> </asp:TemplateField></Columns> </asp:GridView>這是咱們的Code
protected void Page_Load(object sender, EventArgs e){ //if (IsPostBack) { return; }//*3 var item = new[]{ new { id = "i001", name="John"}, new { id = "i002", name="Merry"}, new { id = "i003", name="Ellin"} }; gvList.DataSource = item; gvList.DataBind();//*1 } protected void gvList_RowEditing(object sender, GridViewEditEventArgs e){ gvList.EditIndex = e.NewEditIndex; gvList.DataBind();//*2 Response.Write(e.NewEditIndex); } protected void Page_PreRender(object sender, EventArgs e){ //this.DataBind();//*4 }剛開始, 習慣上
public static void ClickSendValidConfirmDisabled(System.Web.UI.Page page, System.Web.UI.WebControls.WebControl ctrl, string confirmText){ string clientScript = ctrl.ClientID + ".disabled='disabled';" + page.ClientScript.GetPostBackEventReference(ctrl, null) + ";"; bool needValid = false; string validationGroup = null; if (ctrl is System.Web.UI.WebControls.IButtonControl){ System.Web.UI.WebControls.IButtonControl ibtnCtrl = ctrl as System.Web.UI.WebControls.IButtonControl; needValid = ibtnCtrl.CausesValidation; validationGroup = ibtnCtrl.ValidationGroup; } clientScript = "if(confirm('" + confirmText + "')){" + clientScript + "}"; if (needValid){ clientScript = "if(Page_ClientValidate('" + validationGroup + "')){" + clientScript + "}"; } ctrl.Attributes["onclick"] = clientScript + "return false;"; }