在asp.net中从客户端(Javascript)刷新GridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12215384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh GridView from ClientSide (Javascript) in asp.net
提问by Prasad Jadhav
I have added the Gridview control on a webPage.
我在网页上添加了 Gridview 控件。
I am deleting any row (one row at a time) by calling PageMethodas follow:
我通过调用PageMethod删除任何行(一次一行),如下所示:
<script type="text/javascript">
function Delete_Row(){
PageMethods.DeleteRow(row_id, GetTimeCallback, ErrorHandler, TimeOutHandler);
}
GetTimeCallback = function (result)
{
if (result) {
alert('Row is deleted');
// I want to refresh the Gridview here
}
}
<script type="text/javascript">
where "row_id" is primery key of the row.
其中“ row_id”是该行的主键。
It shows the alert perfectly but does not refresh the Gridview with one less deleted row.
what code should i write to Update the gridview?
NOTE:I dont want to refresh entire page.
它完美地显示了警报,但不会用少删除的一行刷新 Gridview。
我应该写什么代码来更新 gridview?
注意:我不想刷新整个页面。
采纳答案by Amol Kolekar
Write CallBackFunction to acheive this...You can find the Callback Functionality at http://msdn.microsoft.com/en-us/library/ms178208and http://msdn.microsoft.com/en-us/library/ms178210
写回调函数,以达致这...你可以找到回调功能http://msdn.microsoft.com/en-us/library/ms178208和http://msdn.microsoft.com/en-us/library/ ms178210
Edit:-
编辑:-
protected void Page_Load(object sender, EventArgs e)
{
String cbReference =Page.ClientScript.GetCallbackEventReference(this,
"arg", "ReceiveServerData", "context");
String callbackScript;
callbackScript = "function CallServer(arg, context)" +
"{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
}
System.IO.StringWriter strDataGridHtml= new System.IO.StringWriter();
public void RaiseCallbackEvent(String eventArgument)
{
string idToBeDeleted=eventArgument;
//Write deleteCode
//DataBind the Grid
HtmlTextWriter htwObject = new HtmlTextWriter(strDataGridHtml);
GridViewControl.RenderControl(htwObject);
}
public String GetCallbackResult()
{
return strDataGridHtml.ToString();
}
Now as you see this strDataGridHtmlwill be sent to Javascript Function ReceiveServerData...
现在,如您所见,此strDataGridHtml将被发送到 Javascript 函数 ReceiveServerData...
<script type="text/ecmascript">
function ReceiveServerData(rValue)
{
document.getElementById("divIDWhichEncapsulategridView").innerHTML = rValue;
}
</script>
Hope this Will Help you..As i don't i have your full code i can't write the exact one...but this should give you some idea on how to proceed...And also please go through the "CallBack" Functionality in order to understand this functionality to the fullest..
希望这会帮助你..因为我没有你的完整代码,我无法写出确切的代码......但这应该让你对如何继续有一些想法......并且还请通过“回调" 功能,以便最充分地了解此功能..