c# - c# img 在 RowDatabound 上应用 onclick 属性
问题描述
在我的 c# gridview中,可以编辑单行。
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton
ID="imgbtnEdit"
CommandName="Edit"
runat="server"
ImageUrl='<%# Eval("Stopped").ToString().Equals("1") ?
"/aspnet/img/edit_1.gif" :
"/aspnet/img/edit_2.gif" %>'
OnClientClick="if (!confirm('Confirm ?')) return false;" />
</ItemTemplate>
</asp:TemplateField>
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
string sID = gv.DataKeys[e.NewEditIndex].Value.ToString();
string queryString = "Default2.aspx?sID=" + sID.ToString();
string newWin = "var Mleft = (screen.width/2)-(1200/2);
var Mtop = (screen.height/2)-(700/2);
window.open('" + queryString + "','_blank',
'height=600,width=600,
status=yes,toolbar=no,scrollbars=yes,menubar=no,
location=no,top=\'+Mtop+\',
left=\'+Mleft+\';');";
ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
}
在rowdatabound 事件中,当第二个单元格的值等于零时,我添加了此条件:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton img = (ImageButton)e.Row.FindControl("imgbtnEdit");
if (e.Row.Cells[2].Text.Equals("0"))
{
img.ImageUrl = "/aspnet/img/stop.gif";
img.Width = 18;
img.Height = 18;
img.Enabled = true;
e.Row.Cells[1].Attributes.Add("onClick", "alert('Cell 1 Clicked');");
}
}
}
但是单击图像stop.gif
首先打开带有Confirm?
消息的警报,最后打开带有消息的警报,Cell 1 Clicked
并在弹出窗口中打开用于编辑行的 asp 页面Default2.aspx 。
在这种情况下,我需要:
- 只打开警报
Cell 1 Clicked
; - 打开新的 aspx 页面Default3.aspx。
如何解决这个问题?
预先感谢您的任何帮助。
解决方案
我希望我是有帮助的。
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
int xCount = Int32.Parse(gv.Rows[e.NewEditIndex].Cells[2].Text);
if (xCount > 0)
{
string sID = gv.DataKeys[e.NewEditIndex].Value.ToString();
string queryString = "Default2.aspx?sID=" + sID.ToString();
string newWin = "var Mleft = (screen.width/2)-(1200/2);
var Mtop = (screen.height/2)-(700/2);
window.open('" + queryString + "','_blank',
'height=600,width=600,
status=yes,toolbar=no,scrollbars=yes,menubar=no,
location=no,top=\'+Mtop+\',
left=\'+Mleft+\';');";
ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
}
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton img = (ImageButton)e.Row.FindControl("imgbtnEdit");
if (e.Row.Cells[2].Text.Equals("0"))
{
img.ImageUrl = "/aspnet/img/stop.gif";
img.Width = 18;
img.Height = 18;
img.Enabled = true;
img.OnClientClick = "";
img.Attributes["onclick"] = "if(!confirm('...')){return false;};";//open Default3.aspx
}
}
}
推荐阅读
- php - 向现有数组添加新键值
- javascript - 在 AG-Grid treeData 中包装树数据项文本
- python - Python:TypeError:不可散列的类型:MongoDB查询时的'list'
- reactjs - map函数导致无限循环
- python - 为什么我不能删除原来的 werkzeug 处理程序?
- macos - macOS 10.14 内核调试
- javascript - NodeJs 服务器充斥着 UDP 广播,不发送响应
- css - text-decoration:内有转换元素的下划线
- reactjs - React Router v4:防止导航返回
- javascript - 路由查询参数更改时,角度材质选项卡导航失去活动标记