none
ASP.NET Getting Started FAQ: Bagaimana untuk memperbarui data pada halaman lain? RRS feed

  • Diskusi Umum

  • Bagaimana anda mengubah data pada halaman dan memperbarui data yang terkait pada halaman lain?

    Misalnya,
    anda mempunyai dua halaman. Salah satunya adalah halaman utama dan lainnya adalah halaman popup. Halaman Popup dibuka oleh halaman utama melalui 'window.open ()'. 

     

    Main page:

    <form id="form2" runat="server"> 

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 

    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 

    <asp:Button ID="Button1" runat="server" Text="Button" /> 

    </form>

     

     private void Page_Load(object sender, EventArgs e) 
    { 
        //Create client side function for update 
        StringBuilder str = new StringBuilder(); 
        str.Append("&lt;script language=javascript>"); 
        str.Append("function update(e){"); 
        str.Append("document.getElementById('" + TextBox1.ClientID + "').value=e[0];"); 
        str.Append("document.getElementById('" + TextBox2.ClientID + "').value=e[1];}"); 
        str.Append("&lt;/script>"); 
        // register the JavaScript into the Page 
        if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "update")) 
        { 
            Page.RegisterClientScriptBlock("update", str.ToString()); 
        } 
        Button1.Attributes.Add("onclick", "window.open('Popup.aspx',null,'left=400, top=100,  height=250, width= 250, status=no, resizable= no, scrollbars= no, toolbar= no,location= no, menubar= no');"); 
    }  

      

    Popup.aspx:    

     

    <form id="form3" runat="server"> 

    <div> 

        <input id="Text1" type="text" /><input id="Text2" type="text" /> 

        <asp:Button ID="Button1" runat="server" Text="Button"  /> 

    </div> 

    </form>

     

    private void Page_Load(object sender, System.EventArgs e) 
    { 
        string str = @"&lt;script language=javascript> 
        function toValue() 
            {   
            var text1=document.getElementById('Text1').value; 
            var text2=document.getElementById('Text2').value; 
            var ar= new Array(text1,text2); 
            window.opener.update(ar);         
            window.close();  
            }   
           </script>"; 
        if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "toValue")) 
        { 
            ClientScript.RegisterClientScriptBlock(this.GetType(), "toValue", str.ToString()); 
        } 
        Button1.Attributes.Add("onclick", "toValue();"); 
    } 

     

     Thread terkait: http://forums.asp.net/t/1200262/2089796.aspx


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Rabu, 27 Juli 2011 03.31
    Moderator