RadGrid 中 点击编辑某条记录,弹出窗口, 关闭窗口后,想让RadGrid自动刷新一下, 大家碰到过不?
官方网站上有专门的说明文档:
Making postback/AJAX request from javascript in user control/webform/MasterPage
一、“用户控件”的使用。
后台代码:
C# | |
---|---|
public class WebUserControl1 : System.Web.UI.UserControl, IPostBackEventHandler
{ public void RaisePostBackEvent(string eventArgument) { switch(eventArgument) } |
前台代码:
var radGrid1_ClientID = "<%=RadGrid1.ClientID %>";
注意:这里取的是控件页面的UniqueID,如果是WebForm页面,这里的值应为"<%=RadGrid1.ClientID %>"
var radGrid1_UniqueID = "<%=this.UniqueID %>";
window[radGrid1_ClientID].AjaxRequest(radGrid1_UniqueID, "Rebind");
参数说明:
__doPostBack(eventTarget, eventArgument) or RadGridClientObject.AjaxRequest(eventTarget, eventArgument) |
||
eventTarget | The control which should raise postback event. If this control is directly on the HtmlForm, you can use the control's UniqueID or ClientID. Otherwise if your control is in a INamingContainer, you must use the control's UniqueID. For UserControl you should use always the user control's UniqueID | |
eventArgument | This is optional argument for the event, used for passing some data on the server (which can be processed further) |
eventArgument:主要用来判断是从哪个地方返回来刷新数据的。
二、MasterPage页面的使用
1.如果RadGrid在asp:ContentPlaceHolder 里:
后台代码:
C# | |
---|---|
public partial class MasterPage : System.Web.UI.MasterPage, IPostBackEventHandler
{ public void RaisePostBackEvent(string eventArgument) { switch (eventArgument) { case "Rebind": RadGrid1.Rebind(); break; } } } |
前台代码:
<title>Untitled Page</title>
<script type="text/javascript">
function RowDblClick(index)
{
grid.AjaxRequest("<%= this.UniqueID %>", "Rebind");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
var grid;
function GridCreated()
{
grid = this;
}
</script>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<rad:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1"
runat= "server" AutoGenerateColumns="True">
<MasterTableView DataSourceID="AccessDataSource1">
</MasterTableView>
<ClientSettings>
<ClientEvents OnGridCreated="GridCreated" OnRowDblClick="RowDblClick"></ClientEvents>
</ClientSettings>
</rad:RadGrid>
<asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Nwind.mdb"
SelectCommand= "SELECT TOP 10 CustomerID, CompanyName, ContactName, ContactTitle,
Address, PostalCode FROM Customers" runat="server"></asp:AccessDataSource>
</asp:ContentPlaceHolder>
2.如果RadGrid在asp:Content里:
function PerformRequest()
{
window["<%= RadGrid1.ClientID %>"].AjaxRequest("<%= RadGrid1.UniqueID %>", "MyArgument");
}
</script>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" ID="Holder1" runat="server">
<asp:CheckBox ID="chkIsClosed" runat="server" OnPreRender="chkIsClosed_PreRender" />
</asp:Content>
<asp:Content ContentPlaceHolderID = "ContentPlaceHolder2" ID="Holder2" runat="server">
<rad:radgrid id="RadGrid1" datasourceid="AccessDataSource1" runat="server"
EnableAJAX="true">
<MasterTableView DataSourceID="AccessDataSource1"></MasterTableView>
</rad:radgrid>
<asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Nwind.mdb"
SelectCommand="SELECT TOP 10 CustomerID, CompanyName, ContactName, ContactTitle,
Address, PostalCode FROM Customers" runat="server"></asp:AccessDataSource>
</asp:Content>
后台代码:
C# | |
---|---|
protected void chkIsClosed_PreRender(object source, System.EventArgs e)
{ (source as CheckBox).Attributes("onclick") = "PerformRequest()"; } |
三、ASPX页面的使用
后台代码:
C# | |
---|---|
protected override void RaisePostBackEvent(IPostBackEventHandler source, String eventArgument)
{ base.RaisePostBackEvent(source, eventArgument); switch(eventArgument) { case "Rebind": RadGrid1.Rebind(); break; } } |
前台代码:
EnableAJAX= "True" AllowSorting="True" AllowPaging="True">
</rad:radgrid>
<br />
<input type=button value="Refresh" onclick='window["<%= RadGrid1.ClientID %>"].AjaxRequest( "<%= RadGrid1.UniqueID%>", "Rebind")' />
RadGrid 中的很多功能需要再仔细的挖掘,希望对大家的工作有所帮助。
欢迎交流。