2011-05-05 72 views
1

我有一個簡單的用戶控件來將數據插入到數據庫中。我爲用戶控件創建了一個名爲isInModifyMode的公共屬性。如果在父窗體控件的click事件上單擊編輯按鈕爲true,然後在父頁面的頁面加載事件上,將usercontrols頁面載入的屬性設置爲true我檢查了屬性IsinModifymode並呈現與填充文本框中的值和所有其他控件從修改數據庫模式的控制(即我更改按鈕的Text屬性更新,而不是保存)有條件地呈現用戶控件

但代碼不能正常工作。如果有任何機構可以提前幫助,請提前致電 。

+0

你能否提供一些代碼片段。根據你的描述很難理解這個問題。你說,「...父窗體控件的點擊事件...」我相信你的意思是在父窗體上的按鈕上的點擊事件,但我不是100%確定的。 – GunnerL3510 2011-05-05 07:00:47

+0

謝謝GunnerL3510我所做的更改後,以使其更容易理解 – Devjosh 2011-05-05 08:35:24

回答

2

它仍然不是很清楚你想要做什麼,究竟是什麼不起作用。但我會在若干假設回答取刺:

  • 你想要的格式轉換隻能在一個簡單的回發事件
  • 通過「父窗體控件的Click事件」基於關閉isInModifyMode的你說的是一鍵觸發回傳
  • 您正在使用C#和代碼屁股

原理很簡單,有許多方法可以做到什麼,我想你問。一種方法是基本上將事件綁定到按鈕以設置控件的isInModifyMode屬性。在控制本身中,讓屬性的setter調用一個更新顯示的方法。這在下面的非常簡陋和低於完美的例子所示:

Default.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <uc1:WebUserControl ID="WebUserControl1" runat="server" isInModifyMode="false" /> 
     <asp:Button ID="EditButton" runat="server" Text="Edit" /> 
    </form> 
</body> 
</html> 

Default.aspx.cs

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected override void OnInit(EventArgs e) 
    { 
     // Assign the handler for the edit button click event 
     EditButton.Click += new EventHandler(EditButton_Click); 

     base.OnInit(e); 
    } 

    /// <summary> 
    /// Edit button click handler 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    void EditButton_Click(object sender, EventArgs e) 
    { 
     WebUserControl1.isInModifyMode = true; 
    } 
} 

WebUserControl1 .ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %> 

<asp:PlaceHolder ID="ReadOnlyModePlaceholder" runat="server"> 
    <p>Read Only Mode</p> 
</asp:PlaceHolder> 

<asp:PlaceHolder ID="EditModePlaceholder" runat="server" Visible="false"> 
    <p>Edit Mode</p> 
</asp:PlaceHolder> 

WebUserControl1.ascx.cs

using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

public partial class WebUserControl : System.Web.UI.UserControl 
{ 
    /// <summary> 
    /// Changes the display mode by toggling the appropriate controls on or off 
    /// based on the isInModifyMode property 
    /// </summary> 
    private void SetDisplayMode() 
    { 
     if (!this.isInModifyMode) 
     { 
      // Render as not modify 
      ReadOnlyModePlaceholder.Visible = true; 
      EditModePlaceholder.Visible = false; 
     } 
     else 
     { 
      // Render as modify 
      ReadOnlyModePlaceholder.Visible = false; 
      EditModePlaceholder.Visible = true; 
     } 
    } 

    /// <summary> 
    /// Determines whether or not the control should be displayed in edit mode or not. 
    /// </summary> 
    public bool isInModifyMode 
    { 
     get 
     { 
      if (ViewState["isInModifyMode"] != null) 
       return (bool)ViewState["isInModifyMode"]; 
      else 
       return false; 
     } 
     set 
     { 
      ViewState["isInModifyMode"] = value; 
      // Since we're possibly changing modes, call 
      // the SetDisplayMode() method to update the display 
      SetDisplayMode(); 
     } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     // Set the display mode on the initial load 
     if (!IsPostBack) 
      SetDisplayMode(); 
    } 
} 
+0

感謝kiddailey如果worls andlet你千牛超重,我會嘗試此解決方案 – Devjosh 2011-05-05 09:26:50

1

看一看在ASP.Net page life cycle和事件順序。在ButtonClick前

頁面加載和控制系統負載事件被觸發。將渲染邏輯移至PreRender事件。

如果這沒有幫助,添加一些代碼,以便更好地理解這個問題的。

+0

相信我會盡力謝謝 – Devjosh 2011-05-05 09:28:06