Telerik RadWindow Javascript 将值返回给 ASP.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2037801/
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
Telerik RadWindow Javascript return values to ASP.NET
提问by SetiSeeker
I have a parent page that launches a telerik radwindow and passes it an argument.
我有一个父页面,它启动一个 Telerik radwindow 并传递一个参数。
Once the radwindow is done processeing the value, I need it to return it to the parent page, and I would like the parent page to have access to this value in my code behind page.
一旦 radwindow 完成对值的处理,我需要它将它返回到父页面,并且我希望父页面能够在我的代码隐藏页面中访问该值。
I have tried to pass the value to a hidden field on my page and then trigger a page refresh and my code behind watches to see if the value is working.
我试图将值传递给我页面上的一个隐藏字段,然后触发页面刷新和我的代码背后的监视以查看该值是否有效。
I can't seem to get this to work. I get the return value in the parent javascript, but i can't get it from my hidden field from the code behind.
我似乎无法让它发挥作用。我在父 javascript 中获得了返回值,但是我无法从后面的代码中的隐藏字段中获得它。
I even get it into the text box like i need to but, when i find the Hidden field in the codebehind, there is no value set.
我什至像我需要的那样将它放入文本框中,但是,当我在代码隐藏中找到隐藏字段时,没有设置任何值。
Where I have set alerts, I am getting the values displayed as i need to.
在我设置警报的地方,我会根据需要显示值。
I suspect that the reason I can't see my return value in the code behind file, is that when the page is refreshed, I am getting a new page and not only causing a post back.
我怀疑我在代码隐藏文件中看不到我的返回值的原因是当页面刷新时,我得到了一个新页面,而不仅仅是导致回发。
And is there not a better way i can do this.
有没有更好的方法可以做到这一点。
here is my code in the parent page:
这是我在父页面中的代码:
Parent ASPX:
父 ASPX:
<script type="text/javascript">
function OpenWnd() {
var oWnd = radopen(null, "RadWindow1");
}
function OnClientShow(oWnd) {
//Create a new Object to be used as an argument to the radWindow
var arg = new Object();
//Using an Object as a argument is convenient as it allows setting many properties.
arg.text = document.getElementById("TextBox1").value;
//Set the argument object to the radWindow
oWnd.Argument = arg;
}
function ClientCallBackFunction(radWindow, returnValue) {
//check if a value is returned from the dialog
if (returnValue.newtext) {
document.getElementById("Hidden1").value = returnValue.newtext;
alert("HiddenValue: " + document.getElementById("Hidden1").value);
}
}
</script>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<div>
<telerik:RadWindowManager ID="RadWindowManager2" runat="server">
<Windows>
<telerik:RadWindow ID="RadWindow1" runat="server" OnClientShow="OnClientShow" ClientCallBackFunction="ClientCallBackFunction"
NavigateUrl="Dialog2.aspx" />
</Windows>
</telerik:RadWindowManager>
</div>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
<input type="button" value="Send content to dialog page" onclick="OpenWnd()" />
<p>
<input id="Hidden1" type="hidden" runat="server" />
</p>
</form>
Parent Code Behind:
背后的父代码:
protected void Page_Load(object sender, EventArgs e)
{
HtmlInputHidden hidden = (HtmlInputHidden)Page.FindControl("Hidden1");
if (IsPostBack && !string.IsNullOrEmpty(hidden.Value))
{
//Code Here
}
}
Here is my Dialog code:
这是我的对话框代码:
Dialog ASPX:
对话框 ASPX:
<script type="text/javascript">
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
function ConfigureDialog() {
//Get a reference to the radWindow wrapper
var oWindow = GetRadWindow();
//Obtain the argument
var oArg = oWindow.Argument;
//Use the argument
var oArea = document.getElementById("TextBox1");
oArea.value = oArg.text;
}
function SendAndClose() {
var oWindow = GetRadWindow();
//Get current content of text area
var arg = new Object();
arg.newtext = document.getElementById("TextBox1").value;
oWindow.Close(arg);
RefreshParentPage();
}
function RefreshParentPage() {
GetRadWindow().BrowserWindow.location.reload();
alert("RefreshParentPage");
}
</script>
Thanks for all the help
感谢所有的帮助
Ian
伊恩
回答by Richard Friend
You are doing the following
您正在执行以下操作
GetRadWindow().BrowserWindow.location.reload();
But that wont cause a postback it will simply reload the parent page, you need to cause a potback. You could try adding a button to the parent form with the style set 'display:none', and handling the click event in the code behind, you can fire this button off from your js code.
但这不会导致回发,它只会重新加载父页面,您需要导致回发。您可以尝试使用样式集“display:none”向父表单添加一个按钮,并在后面的代码中处理 click 事件,您可以从您的 js 代码中触发此按钮。
In Parent Page :
在父页面中:
<asp:Button runat="server" id="btnClick" Style="display:none" OnClick="btnClick_Click"/>
protected void btnClick_Click(object sender,EventArgs e)
{
string val = this.Hidden1.Value; //Code goes here
}
You can invoke from your javascript like this (non jQuery), place this in your callback
您可以像这样(非 jQuery)从您的 javascript 调用,将其放在您的回调中
document.getElementById('<%= btnClick.ClientID').click();
回答by Jeff
A better approach would be this on the aspx side:
更好的方法是在 aspx 方面:
<%=this.ClientScript.GetPostBackEventReference(new System.Web.UI.PostBackOptions(btnClick))%>
<%=this.ClientScript.GetPostBackEventReference(new System.Web.UI.PostBackOptions(btnClick))%>

