locked
Partial Page Update RRS feed

  • Question

  • User-1528972981 posted

    Hi

    I am very much new to Ajax. I have created my ASP.NET web application. I have a asp text box and a html button.

    On click of this button I am opening a child pop up and then passing the value from pop up back to parent page using Javascript.

    Now everytime I do this I want a partial page update / a async post back to the server so that the value is restored. Incase the page refreshes accidentally, I am loosing the value [:(].

    I have tried using the Script Manager and the Update Panel with a trigger for Text Changed event for the asp text box.

    But nothing seems to be happeining [:(].

    Any suggestions on what could be done to save the values and avoid a complete page refresh.

     

    Thanks

     

    Friday, January 23, 2009 4:53 PM

Answers

  • User-334687597 posted

     Hi Badri ,

    Try the following code . Here from Form1 I am opening form2 and then passing information back to form1 without postback . Also I am causing postback in parent form ie form1 from form2 , so that value gets retained during accidental postback .

    Code :-

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Form1.aspx.cs" Inherits="Form1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Form 1</title>
        <script language="javascript" type="text/javascript">

            function pageLoad() {
                $addHandler($get('btnPopUp'), 'click', onClick);
            }

            function onClick(evt) {
                var browser;
                if (!browser) {
                    browser = window.open('Form2.aspx?ctrlID=txtContent', 'browser','resizable=0,scrollbars=1,width=710,height=380');
                }
            }

            function Submit() {
                __doPostBack('txtContent', '');
            }
           
       
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
        <div>
            <table style="width:100%;">
                <tr>
                    <td>
                        <asp:TextBox ID="txtContent" runat="server" Width="308px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" ID="btnPopUp" runat="server" value="Open Pop Up" Width="160px" />
                        <asp:Button ID="btnRefresh" runat="server" Text="Refresh" Width="145px"
                            onclick="btnRefresh_Click" />
                    </td>
                </tr>
            </table>
        </div>
        </form>
    </body>
    </html>

    Form1.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Form1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {

        }
    }

    Form2.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Form2.aspx.cs" Inherits="Form2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
       
            <table style="width:100%;">
                <tr>
                    <td>
                        <asp:TextBox ID="txtClose" runat="server" Width="254px"></asp:TextBox>
                        <asp:Button ID="btnClose" runat="server" onclick="btnClose_Click" Text="Close"
                            Width="97px" />
                    </td>
                </tr>
            </table>   
        </div>
        </form>
    </body>
    </html>

    Form2.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;

    public partial class Form2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnClose_Click(object sender, EventArgs e)
        {
            // declare the variable to getelementID
            string strStart = "document.getElementById('";
            string strend = "')";

            String CtrlID = HttpContext.Current.Request.QueryString.Get("ctrlID");

            CtrlID = strStart + CtrlID + strend;

            StringBuilder strPassValuesBack = new StringBuilder();
            strPassValuesBack.Append("<script language=\"javascript\">");
            strPassValuesBack.Append("window.opener.");
            strPassValuesBack.Append(CtrlID);
            strPassValuesBack.Append(".value = '");
            strPassValuesBack.Append(txtClose.Text);
            strPassValuesBack.Append("';window.close();");
            strPassValuesBack.Append("window.opener.Submit();");
            strPassValuesBack.Append("</script>");

           
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PassValuesToParents", strPassValuesBack.ToString(), false);
        }
    }
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 25, 2009 1:47 PM

All replies

  • User1281381861 posted

     Hi,badri

    Would U Show your code Please .

    Friday, January 23, 2009 11:19 PM
  • User-334687597 posted

     Hi Badri ,

    Try the following code . Here from Form1 I am opening form2 and then passing information back to form1 without postback . Also I am causing postback in parent form ie form1 from form2 , so that value gets retained during accidental postback .

    Code :-

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Form1.aspx.cs" Inherits="Form1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Form 1</title>
        <script language="javascript" type="text/javascript">

            function pageLoad() {
                $addHandler($get('btnPopUp'), 'click', onClick);
            }

            function onClick(evt) {
                var browser;
                if (!browser) {
                    browser = window.open('Form2.aspx?ctrlID=txtContent', 'browser','resizable=0,scrollbars=1,width=710,height=380');
                }
            }

            function Submit() {
                __doPostBack('txtContent', '');
            }
           
       
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
        <div>
            <table style="width:100%;">
                <tr>
                    <td>
                        <asp:TextBox ID="txtContent" runat="server" Width="308px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" ID="btnPopUp" runat="server" value="Open Pop Up" Width="160px" />
                        <asp:Button ID="btnRefresh" runat="server" Text="Refresh" Width="145px"
                            onclick="btnRefresh_Click" />
                    </td>
                </tr>
            </table>
        </div>
        </form>
    </body>
    </html>

    Form1.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Form1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {

        }
    }

    Form2.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Form2.aspx.cs" Inherits="Form2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
       
            <table style="width:100%;">
                <tr>
                    <td>
                        <asp:TextBox ID="txtClose" runat="server" Width="254px"></asp:TextBox>
                        <asp:Button ID="btnClose" runat="server" onclick="btnClose_Click" Text="Close"
                            Width="97px" />
                    </td>
                </tr>
            </table>   
        </div>
        </form>
    </body>
    </html>

    Form2.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;

    public partial class Form2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnClose_Click(object sender, EventArgs e)
        {
            // declare the variable to getelementID
            string strStart = "document.getElementById('";
            string strend = "')";

            String CtrlID = HttpContext.Current.Request.QueryString.Get("ctrlID");

            CtrlID = strStart + CtrlID + strend;

            StringBuilder strPassValuesBack = new StringBuilder();
            strPassValuesBack.Append("<script language=\"javascript\">");
            strPassValuesBack.Append("window.opener.");
            strPassValuesBack.Append(CtrlID);
            strPassValuesBack.Append(".value = '");
            strPassValuesBack.Append(txtClose.Text);
            strPassValuesBack.Append("';window.close();");
            strPassValuesBack.Append("window.opener.Submit();");
            strPassValuesBack.Append("</script>");

           
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PassValuesToParents", strPassValuesBack.ToString(), false);
        }
    }
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 25, 2009 1:47 PM