最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

doPostBack from C# with JavaScript - Stack Overflow

programmeradmin1浏览0评论

hi I have one parent page which opens a pop up window, and user makes some changes on child pop up page then clicks a save button. When the user clicks the save button, I want to doPostBack to the parent page so that the changes made in the pop up window can be seen in parent window.

Question : How can I achive the above scenario?

I want to write the script code in aspx.cs file, I tried

string script = "";
script = "<script>window.opener.__doPostBack('UpdatePanel1', '')</script>";
ScriptManager.RegisterClientScriptBlock(Literal1, typeof(Literal), "yenile", script, true);

but this did not do anything, no errors just nothing.

I am new to JavaScript, need help with all steps.

hi I have one parent page which opens a pop up window, and user makes some changes on child pop up page then clicks a save button. When the user clicks the save button, I want to doPostBack to the parent page so that the changes made in the pop up window can be seen in parent window.

Question : How can I achive the above scenario?

I want to write the script code in aspx.cs file, I tried

string script = "";
script = "<script>window.opener.__doPostBack('UpdatePanel1', '')</script>";
ScriptManager.RegisterClientScriptBlock(Literal1, typeof(Literal), "yenile", script, true);

but this did not do anything, no errors just nothing.

I am new to JavaScript, need help with all steps.

Share Improve this question edited Jun 29, 2011 at 22:54 Bastardo asked May 27, 2011 at 15:27 BastardoBastardo 4,1529 gold badges43 silver badges60 bronze badges 1
  • You don't need the script tags, there's a boolean parameter to include them when you call ScriptManager.RegisterClientScriptBlock(...) – nickytonline Commented May 27, 2011 at 17:19
Add a ment  | 

4 Answers 4

Reset to default 2

The parent page:

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <div>
            <asp:Literal runat="server" ID="ChildWindowResult" />
        </div>
        <hr />
        <input type="button" value="Open Dialog" onclick="window.open('MyDialog.aspx', 'Dialog');" />
        <asp:Button ID="HiddenButtonForChildPostback"  runat="server"
            OnClick="OnChildPostbackOccured" style="display: none;" />
        <asp:HiddenField runat="server" ID="PopupWindowResult"/>
    </ContentTemplate>
</asp:UpdatePanel>

The MyDialog page:

<script type="text/javascript" src="http://ajax.aspnetcdn./ajax/jQuery/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
    function postData() {
        var resultField = $("input[type='hidden'][id$='PopupWindowResult']", window.opener.document);
        var parentPosDataButton = $("[id$='HiddenButtonForChildPostback']", window.opener.document);

        resultField.val($("#<%= SomeValueHiddenField.ClientID  %>").val());
        parentPosDataButton.click();
    }
</script>

<asp:TextBox runat="server" ID="SomeValueHiddenField" />
<asp:Button runat="server" OnClick="PostData" Text="Click Me" />

protected void PostData(object sender, EventArgs e)
{
   SomeValueHiddenField.Value = DateTime.Now.ToString();
   ClientScript.RegisterStartupScript(this.GetType(), "PostData", "postData();", true);
}

But I believe that it would be much better to utilize here some pop-up controls like PopUpExtender from the AjaxControlToolkit library or dialog from the jQuery-UI.

You probably need to use ClientID:

string script = "";
script = "<script>window.opener.__doPostBack('" + UpdatePanel1.ClientID + "', '')</script>";
ScriptManager.RegisterClientScriptBlock(Literal1, typeof(Literal), "yenile", script, true);

The last parameter is to whether include script tag or not

So, if you do

RegisterClientScriptBlock(page,type, "<script>foo();</script>", true);

You will end up with:

"<script><script>foo();</script></script>"

So, change your last parameter to false, or better yet, remove the tags in the string

Review the following suggested solution:

http://livshitz.wordpress./2011/06/12/use-popup-to-postbackupdate-its-parentopener-without-losing-viewstate-values-and-close/#more-16

发布评论

评论列表(0)

  1. 暂无评论