我正在使用以下代码..
- 当我点击链接时,将调用 javascript
Hello()
函数 - 我想使用
window.location.href
- 但是当我使用以下
__doPostBack('Button2_Click')
时,它不起作用。 但是,当从以下代码中删除
window.location.href
时,__doPostBack('Button2_Click')
就会起作用。<script type="text/javascript"> function Hello(clicked_id) { var abc = "http://localhost:2621/OrgChart.aspx?id" + clicked_id; window.location.href = abc; __doPostBack('Button2_Click'); return false; } </script> <a id="A1" href="javascript:Hello();">LINK</a>
这是我的代码背后的代码...
public partial class WebForm17 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.GetPostBackEventReference(this, string.Empty);//This is important to make the "__doPostBack()" method, works properly
if (Request.Form["__EVENTTARGET"] == "Button2_Click")
{
//call the method
Button2_Click(this, new EventArgs());
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "Method called!!!";
EmpInfo emp = new EmpInfo();
DA_EmpInfo da_emp = new DA_EmpInfo();
List<EmpInfo> lei = da_emp.GetAllEmployeeInfoByEmpId("MJ-IB-1");
DetailsView1.DataSource = lei;
DetailsView1.DataBind();
}
}
最佳答案
我猜,__doPostBack
正在向服务器发出请求,而您可以使用window.location.href = abc;
来中断它。
您应该使用此请求中的一些回调来重定向到您的网址。
关于javascript - 当我使用 window.location.href 时,我的另一个函数没有调用。以下是我的 javascript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21753408/