我有一个C#Web应用程序。在链接的事件处理程序中,我将名为addUser.ascx的用户控件添加到应用程序的主页,如下所示:
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
}
}
}
答案 0 :(得分:1)
您应该在要更新的部分周围使用 ajax 和 asp:updatePanel 。这里描述了一个非常好的例子:A nice example on asp:updatePanel
答案 1 :(得分:0)
在按钮点击事件中,它始终将页面发送到服务器端,因此每次单击按钮时都会刷新页面。
但你可以使用onclientclick而不是onclick ..所以它不会刷新你的页面..