利用JavaScript打开新的页面,在打开的新页面中传值给上一个页面
很实用的一个方法,在此记录备忘
page1.aspx.cs
protected void Page_Load(object sender, EventArgs e)//页面载入时给按钮注册一个js事件,当点击按钮时打开新页面 { string js = "javascript:w=window.open('page2.aspx?Name="+ this.TextBox1.ClientID +"')"; this.Button1.Attributes.Add("onclick", js); }
page2.aspx.cs
protected void Button1_Click(object sender, EventArgs e)//点击按钮时获取URL传来的控件ID,把要得到到的值赋给传来的URL { string name = Request.QueryString["Name"].ToString(); string value = ((Button)sender).CommandArgument.ToString(); ScriptManager.RegisterStartupScript(this, this.GetType(), "", GetNameTextAndValueToOpener(name,value),true); } public string GetNameTextAndValueToOpener(string name,string value) { string js = "winOpener=window.self.opener;winOpener.document.getElementById('"+ name +"').value='"+ value +"';window.close();"; return js; }