Javascript Return False 的重要性.
<asp:DataGrid>
<Columns>
<asp:TemplateColumn HeaderText="发单">
<itemtemplate>
<asp:Button ID="btnFd" Runat="server" CssClass="Button60" Text="发单" CommandName="btnFd"></asp:Button>
</itemtemplate>
<headerstyle wrap="False" width="60px" />
<itemstyle wrap="False" width="60px" />
</asp:TemplateColumn>
<Columns>
</Datagrid>
<Columns>
<asp:TemplateColumn HeaderText="发单">
<itemtemplate>
<asp:Button ID="btnFd" Runat="server" CssClass="Button60" Text="发单" CommandName="btnFd"></asp:Button>
</itemtemplate>
<headerstyle wrap="False" width="60px" />
<itemstyle wrap="False" width="60px" />
</asp:TemplateColumn>
<Columns>
</Datagrid>
以上是javascript 提示
private void dvList_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//发单按钮
if(e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item)
{
Button btnFd = (Button)e.Item.FindControl("btnFd");
string id = ((DataRowView)e.Item.DataItem).Row["ID"].ToString();
btnFd.Attributes["onclick"] = "return btnFd_onclick('"+id+"');";
}
}
以上是后台代码{
//发单按钮
if(e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item)
{
Button btnFd = (Button)e.Item.FindControl("btnFd");
string id = ((DataRowView)e.Item.DataItem).Row["ID"].ToString();
btnFd.Attributes["onclick"] = "return btnFd_onclick('"+id+"');";
}
}
//发单按钮事件
function btnFd_onclick(id)
{
var url = "PersonSelectFrame.aspx";
var retval= window.showModalDialog(url,window,'DialogWidth:650px;DialogHeight:650px;Status:no;');
if(retval != undefined && retval != null)
{
document.getElementById("hnJsry").value = retval["id"];
document.getElementById("txJsry").value = retval["name"];
document.getElementById("hnDeleteId").value = id;
document.getElementById("hnBtn").click();
}
}
这样的话在执行hnBtn的click事件后继续会返回服务器端,导致此hnBtn的click事件中的脚本不执行.function btnFd_onclick(id)
{
var url = "PersonSelectFrame.aspx";
var retval= window.showModalDialog(url,window,'DialogWidth:650px;DialogHeight:650px;Status:no;');
if(retval != undefined && retval != null)
{
document.getElementById("hnJsry").value = retval["id"];
document.getElementById("txJsry").value = retval["name"];
document.getElementById("hnDeleteId").value = id;
document.getElementById("hnBtn").click();
}
}
需要在btnFd_onclick(id)后添加一个 return false;//it is very impossible; 添加后这个才ok.