简体   繁体   中英

asp.net C# update textbox value without refresh

I have a website where there are two pages, page1 contain textbox and button1,

if I cliked button1 it will open page2 which contain button2,

if I cliked button2 it will assign value from page2 to the textbox in page1

but the problem is the value will display in the textbox after I refresh page1,

and my question is how I can update textbox value directly without refresh in page1 after I click button2 in page2 like this:

here is my code:

page1 aspx.cs:

<script type="text/javascript">

function openPopup() {

    window.open("page2.aspx", "_blank", "WIDTH=1080,HEIGHT=790,scrollbars=no, menubar=no,resizable=yes,directories=no,location=no");

}

<asp:button text="clik" id="button1" runat="server" onclientclick="return openPopup()" xmlns:asp="#unknown" style="margin-right:30%" />



    protected void Page_Load(object sender, EventArgs e)
    {

       if (Session["userID"] != null)
                {
                    txtbox.Text = HttpContext.Current.Session["userID"].ToString();
                }
    }

page2:

   protected void button2(object sender, EventArgs e)
    {
      Session["userID"] = row.Cells[0].Text;
    }

we can do this in following way:

  1. create a javascript function on page 2, that refresh textbox1 on page 1.
  2. on button2 click from page 2, using scriptmanager, call the function created in step 1 with require param.

So, basically you code be like:

 // place this in page2.aspx
 // javascript function to update text box in page 1;
  function UpdateParentText(value){  
     if(typeof(value) != undefined){
        window.opener.document.getElementById("TextBox1").value=value;
      }
   }

  //2. code behind call to the javascript function from page2.cs
  protected void button2(object sender, EventArgs e)
    {
    // code behind call to the javascript function. 
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "MyScript", "UpdateParentText('" + row.Cells[0].Text + "'", true);
   }

If you are not using AJAX, then use this:

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", "UpdateParentText('" + row.Cells[0].Text + "'", true);

You may use:

  • window.postMessage() — to send the message
  • window.addEventListener(“message”,callback) — to receive and process the message

See sample here .

thanks everyone I solve it by using session with javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM