每当我尝试从代码隐藏文件调用 JavaScript 函数时,它都不起作用。
根本原因是Ajax更新面板。
我已经尝试了所有常见的解决方案,例如使用 ScriptManager.RegisterStartupScript
等,但仍然不起作用。
下面是我的代码:
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gvTemp = (GridView)sender;
gvUniqueID = gvTemp.UniqueID;
gvTemp.EditIndex = e.NewEditIndex;
GridViewRow gvParentRow = gvTemp.Parent.Parent as GridViewRow;
GridView parent = (GridView)gvParentRow.NamingContainer;
int taskid = Convert.ToInt32(parent.DataKeys[gvParentRow.RowIndex].Value.ToString());
var lstTaskSheets = (List<Entities.TaskSheetManagement>)Session["CurrentUserTaskSheets"];
List<Entities.WorkDone> lstWorkdone = lstTaskSheets.Where(x=>x.TaskSheetId == taskid).SelectMany(c => c.WorkDones).ToList();
gvTemp.DataSource = lstWorkdone;
gvTemp.DataBind();
UpdatePanelGridView.Update();
string id = "div"+ taskid.ToString();
ScriptManager.RegisterStartupScript(this.Page,this.Page.GetType(), "Expand", "<script type='text/javascript'>expandcollapse('div" + taskid.ToString() + "','one');</script>",false);
// ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "message", "expandcollapse(" + id + ",alt)", false); //Ihave also tried this
}
Javascript 代码是:
function expandcollapse(obj, row) {
var div = document.getElementById(obj);
var img = document.getElementById('img' + obj);
if (div.style.display == "none") {
div.style.display = "block";
if (row == 'alt') {
img.src = "../../img/minus.gif";
}
else {
img.src = "../../img/minus.gif";
}
img.alt = "Close to view other Customers";
}
else {
div.style.display = "none";
if (row == 'alt') {
img.src = "../../img/plus.gif";
}
else {
img.src = "../../img/plus.gif";
}
img.alt = "Expand to show Orders";
}
}
更新面板的使用方式如下
<asp:UpdatePanel ID="UpdatePanelGridView" runat="server" UpdateMode="Conditional" >
请大家帮忙!!!
最佳答案
您不需要使用<script>
标签如下:
ScriptManager.RegisterStartupScript(this.Page,this.Page.GetType(), "Expand", "<script type='text/javascript'>expandcollapse('div" + taskid.ToString() + "','one');</script>",false);
替换论证:
"<script type='text/javascript'>expandcollapse('div" + taskid.ToString() + "','one');</script>"
如下:
"expandcollapse('div" + taskid.ToString() + "','one');"
ScriptManager 足够智能,可以附加脚本标签。
希望这有帮助。
关于c# - Ajax 更新面板不允许 javascript 在服务器端工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24819099/