当从ASP.net代码创建和调用JavaScript函数时,JavaScript函数应该是未定义的
我想从代码隐藏中执行一个JavaScript函数(例如,作为服务器端的按钮点击事件),并且触发按钮位于UpdatePanel中。我为此编写了两种方法:当从ASP.net代码创建和调用JavaScript函数时,JavaScript函数应该是未定义的,javascript,asp.net,post,updatepanel,code-behind,Javascript,Asp.net,Post,Updatepanel,Code Behind,我想从代码隐藏中执行一个JavaScript函数(例如,作为服务器端的按钮点击事件),并且触发按钮位于UpdatePanel中。我为此编写了两种方法: public static void Redirect(UpdatePanel updatePanelOrThis, string destinationUrl, NameValueCollection data) { string st
public static void Redirect(UpdatePanel updatePanelOrThis, string destinationUrl,
NameValueCollection data)
{
string strForm = PreparePOSTForm(destinationUrl, data);
ScriptManager.RegisterClientScriptBlock(updatePanelOrThis, updatePanelOrThis.GetType(), "redirectscript",
"<script language='javascript' type='text/javascript'> postToPage();</script>", false);
}
private static String PreparePOSTForm(string url, NameValueCollection data)
{
string jscriptString = "<script language=" + "\"" + "javascript" + "\"" + " type=" + "\"" + "text/javascript" + "\"" + ">" +
"function postToPage() " + "{" + "var form = document.createElement(" + "\"" + "form" + "\"" + ");" +
"form.setAttribute(" + "\"" + "method" + "\"" + ", " + "\"" + "POST" + "\"" + ");" +
"form.setAttribute(" + "\"" + "action" + "\"" + ", " + "\"" + url + "\"" + ");" +
"form.setAttribute(" + "\"" + "target" + "\"" + ", " + "\"" + "_self" + "\"" + ");";
int counter = 0;
foreach (string key in data)
{
jscriptString += "var hiddenField" + counter.ToString() + " = document.createElement(" + "\"" + "input" + "\"" + ");" +
"hiddenField" + counter.ToString() + ".setAttribute(" + "\"" + "name" + "\"" + ", " + "\"" + key + "\"" + ");" +
"hiddenField" + counter.ToString() + ".setAttribute(" + "\"" + "value" + "\"" + ", " + "\"" + data[key] + "\"" + ");" +
"form.appendChild(hiddenField" + counter.ToString() + ");";
counter++;
}
jscriptString += "document.body.appendChild(form);form.submit();document.body.removeChild(form);}</script>";
return jscriptString;
}
公共静态无效重定向(UpdatePanel updatePanelThis,字符串destinationUrl,
名称值(收集数据)
{
字符串strForm=PreparePOSTForm(destinationUrl,数据);
ScriptManager.RegisterClientScriptBlock(updatePanelOrThis,updatePanelOrThis.GetType(),“重定向脚本”,
“postToPage();”,false);
}
私有静态字符串PreparePOSTForm(字符串url、NameValueCollection数据)
{
string JScript字符串=“”+
函数postToPage()“+”{“+”var form=document.createElement(“+”\“+”form“+”\”)+
form.setAttribute(“+”\“+”方法“+”\“+”,“+”\“+”POST“+”\”)+
form.setAttribute(“+”\“+”操作“+”\“+”,“+”\“+url+”\“+”)+
form.setAttribute(“+”\“+”目标“+”\“+”,“+”\“+”\u self“+”\”;”;
int计数器=0;
foreach(数据中的字符串键)
{
JScript字符串+=“var hiddenField”+counter.ToString()+”=document.createElement(“+”\“+”输入“+”\”+”)+
“hiddenField”+counter.ToString()+”.setAttribute(“+”\“+”名称“+”\“+”+”,“+”\“+”+”键+“\”+”)+
“hiddenField”+计数器.ToString()+”.setAttribute(“+”\“+”值“+”\”+”,“+”\”+数据[键]+“\”+”)+
“form.appendChild(hiddenField”+counter.ToString()+”;”;
计数器++;
}
JScript字符串+=“document.body.appendChild(表单);form.submit();document.body.removeChild(表单);}”;
返回JScript字符串;
}
当我调用重定向方法时,我看到
未捕获引用错误:未定义postToPage
浏览器控制台中出现错误
我还使用RegisterStartupScript测试了重定向方法,但错误并没有消失
我的方法有什么问题?我在代码中看到的一个“错误”是,您没有在任何地方使用包含脚本的最终字符串,这一行:
string strForm = PreparePOSTForm(destinationUrl, data);
我在这段代码中看到的一个bug是在这一行
string strForm=PreparePOSTForm(destinationUrl,data)代码>strForm
在任何地方都不会被使用,谢谢@Aristos你的线索解决了问题,然后我给你一个答案。@Aristos当然可以