2012-06-29 99 views
0

我有一個網格視圖在我的主頁和我顯示用戶的一些數據(使用BindGrid方法)。這個網格視圖有一些命令按鈕,每行執行一些操作,如Update。當用戶點擊更新按鈕時,我向他/她展示了一個用戶控件以更新值。當用戶點擊更新時,我希望網格綁定到新數據(我希望爲新數據調用BindGrid)。我如何做到這一點,並從用戶控制調用主頁中的方法?更新從用戶控制主頁

編輯1)

我寫的用戶控件的代碼:

public partial class SomeUserControl : System.Web.UI.UserControl 
{ 
public event EventHandler StatusUpdated; 

private void FunctionThatRaisesEvent() 
{ 
    if (this.StatusUpdated != null) 
     this.StatusUpdated(new object(), new EventArgs()); 
} 


public void Page_Load(object sender, EventArgs e) 
{ 
    //.... 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    FunctionThatRaisesEvent(); 
} 
} 

和用戶控制設計師

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SomeUserControl.ascx.cs" Inherits="SomeUserControl" %> 
<asp:Button ID="Button1" runat="server" Text="Update" Height="70px" 
onclick="Button1_Click" Width="183px" /> 

,並添加該代碼主要頁面:

protected void Page_Load(object sender, EventArgs e) 
{ 

} 
protected void Unnamed1_Click(object sender, EventArgs e) 
{ 
    SomeUserControl userControl = (SomeUserControl)LoadControl("SomeUserControl.ascx"); 
    userControl.StatusUpdated += new EventHandler(userControl_StatusUpdated); 
    Panel1.Controls.Add(userControl); 
} 

void userControl_StatusUpdated(object sender, EventArgs e) 
{ 
    GetDate(); 
} 

private void GetDate() 
{ 
    TextBox1.Text = DateTime.Today.ToString(); 
} 

和設計師主頁:

<%@ Register src="SomeUserControl.ascx" tagname="SomeUserControl" 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></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <div> 
     <asp:UpdatePanel ID="upd1" runat="server" UpdateMode="Conditional"> 
      <ContentTemplate> 
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
       <asp:Button runat="server" Text="Add User Control" Height="44px" ID="Nims" 
       onclick="Unnamed1_Click" Width="133px" /> 
       <asp:Panel ID="Panel1" runat="server" BackColor="#FFFFCC"></asp:Panel> 
      </ContentTemplate> 
     </asp:UpdatePanel> 
    </div> 

    </form> 
</body> 
</html> 

,但它不工作,沒有什麼happend。即使我爲點擊用戶控件按鈕代碼添加斷點,但似乎事件不會升高。

回答

1

從用戶控件中引發事件,並從主頁面處理事件。檢查出this question

// **編輯** //

您在點擊按鈕動態地添加用戶控件。當您單擊用戶控件上的按鈕時,它將首先在主頁面上啓動回發 - 現在您的用戶控件不再存在(這就是爲什麼不會引發事件)。如果你改變你的主要頁面設計看起來像這樣:

<%@ Register Src="SomeUserControl.ascx" tagname="SomeUserControl" 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 id="Head1" runat="server"> 
    <title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<div> 
    <asp:UpdatePanel ID="upd1" runat="server" UpdateMode="Conditional"> 
     <ContentTemplate> 
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
      <asp:Button runat="server" Text="Add User Control" Height="44px" ID="Nims" 
      onclick="Unnamed1_Click" Width="133px" /> 
      <asp:Panel ID="Panel1" runat="server" BackColor="#FFFFCC"> 
      <uc1:SomeUserControl ID="userControl" runat="server" /> 
      </asp:Panel> 
     </ContentTemplate> 
    </asp:UpdatePanel> 
</div> 

</form> 

和代碼隱藏看起來像這樣

protected void Page_Load(object sender, EventArgs e) 
{ 
    userControl.StatusUpdated += new EventHandler(userControl_StatusUpdated); 
} 

void userControl_StatusUpdated(object sender, EventArgs e) 
{ 
    GetDate(); 
} 

private void GetDate() 
{ 
    TextBox1.Text = DateTime.Today.ToString(); 
} 

你會明白我的意思。嘗試在主頁和用戶控件的頁面加載事件上設置斷點,以準確查看事件發生的順序。

+0

請參閱編輯1 – Arian

+0

編輯答案 – Goose

0

你需要使用該

事件處理程序在你的ascx頁面中定義事件處理

public event EventHandler ButtonClickDemo; 

當您正在執行更新或刪除事件使用了事件處理程序有以下代碼。

ButtonClickDemo(sender, e); 
在父頁使用

以下

protected void Page_Load(object sender, EventArgs e) 
    { 
     btnDemno.ButtonClickDemo += new EventHandler(btn_Click); 
    } 

結合父頁面

protected void btn_Click(object sender, EventArgs e) 
    { 
      BindGrid(); 
    } 

上述代碼的網格將調用父頁面的BindGrid函數的函數。