[英]Update Main page from user control
我的主頁上有一個網格視圖,並為用戶顯示一些數據(使用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>
但是它不起作用,什么也沒發生。 甚至我為單擊用戶控件按鈕代碼添加了斷點,但似乎事件並未引發。
從用戶控件引發事件,並從主頁處理事件。 看看這個問題 。
// **編輯** //
您將在單擊按鈕時動態添加用戶控件。 當您單擊用戶控件上的按鈕時,它將首先在主頁上發起回發-現在您的用戶控件不再存在(這就是為什么不引發事件)。 如果將主頁設計器更改為以下形式:
<%@ 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();
}
您會明白我的意思。 嘗試在主頁和用戶控件的頁面加載事件上設置斷點,以准確查看事情發生的順序。
您可以在用戶控件中注冊事件,在用戶單擊更新按鈕時引發事件,並在頁面上捕獲該事件。
您需要為此使用事件處理程序
在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函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.