我有一個C#web應用程序。在鏈接的事件處理程序,我添加一個名爲addUser.ascx到應用程序的主頁的用戶控件如下:C#:每次點擊一個按鈕時都會重新加載網頁
addUser au = (addUser)LoadControl("UserControls/addUser.ascx");
content.Controls.Add(au);
的問題是,當我點擊用戶控件的一個按鈕,整個頁面將被刷新並刪除了用戶控件。我該如何解決這個問題?在用戶控制按鈕定義如下:
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" Width="125px" Height="30px" BackColor="Green" Font-Bold="False" Font-Names="Tahoma" TabIndex="7" />
編輯: Home.aspx
<%@ Page Title="Home" Language="C#" MasterPageFile="~/Site.Master" CodeBehind="Home.aspx.cs" Inherits="WebApplication1.Home" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI"%>
<%@ Register TagPrefix="au" TagName="AddUser" Src="~/UserControls/addUser.ascx" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<telerik:RadSkinManager ID="QsfSkinManager" runat="server" ShowChooser="true" />
<div>
<asp:LinkButton ID="AddLink" OnClick="AddLink_Click" runat="server" Text="Add New User" />
<telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource" PageSize="10">
<MasterTableView DataKeyNames="Id" Font-Names="Tahoma" Dir="RTL">
<Columns>
<telerik:GridBoundColumn DataField="UserName" HeaderText="User Name" />
<telerik:GridBoundColumn DataField="Password" HeaderText="Full Name" />
</Columns>
</MasterTableView>
<PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>
</div>
<asp:PlaceHolder runat="server" ID="content" />
</asp:Content>
Home.aspx.cs
namespace WebApplication1
{
public partial class Home : Page
{
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = new Domain().getAllUsers();
}
// some irrelevent methods here
//
protected void AddLink_Click(object sender, EventArgs e)
{
content.Controls.Clear();
addUser au = (addUser)LoadControl("UserControls/addUser.ascx");
content.Controls.Add(au);
}
}
}
ADDUSER .ascx
個<%@ Control Language="C#" CodeBehind="addUser.ascx.cs" Inherits="WebApplication1.UserControls.addUser" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<div>
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell>
<div style="float: right;">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click"/>
</div>
</asp:TableCell>
<asp:TableCell>
<div style="float: right;">
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click"/>
</div>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
addUser.ascx.cs
namespace WebApplication1.UserControls
{
public partial class addUser : System.Web.UI.UserControl
{
protected void btnCancel_Click(object sender, EventArgs e)
{
this.Visible = false;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//add somthing to db
// when this button clicked, the whole page will be
// refreshed and the addUser user control will be removed
}
}
}
您應該先閱讀ASP.NET Web窗體的工作方式以及頁面生命週期和回發的內容。 – dotnetom