gridview中模板列中绑定了LinkButton,RowDataBound事件中给LinkButton添加脚本((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('btnl').click();");'btnl'为LinkButton自己的ID时找不到对象,提示document.getElementById( )对象为空,是不是gridview中的button不能注册自己的click()方法啊??当注册其他按钮的click事件时正常运行,代码如下:((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('btn2').click();"); 事件代码
protected void btn2_Click(object sender, EventArgs e)
{ txt1.Text = "click2";
}程序正常运行,设断点查看也正常,但就是页面上文本框里不显示值,不知道是什么原因??
protected void btn2_Click(object sender, EventArgs e)
{ txt1.Text = "click2";
}程序正常运行,设断点查看也正常,但就是页面上文本框里不显示值,不知道是什么原因??
解决方案 »
- 怎样修改html对象,例如<td />的属性title的显示时间?
- 使用Microsoft.VisualBasic.Strings.StrConv转换水晶报表,导出Excel为乱码
- 关于web路径的问题
- 请问添加和修改记录时,用存储过程和用sql 语句 在性能上有区别吗?
- 请问:ActiveReport怎样才能同时邦定一个DateSet的两张表中的数据(急)
- 关于:asp.net实现 tab,每一个标签下是一个gridview,gridview中有一个checkbox列用来多选删除等。
- ASP.Net技术交流中心的群号
- 打开一个新窗口来显示指定的页面,服务器端的代码怎么写?
- 此问题绝不简单!!!
- 组件和控件有什么区别?
- button 提交
- vs 2003 error CS1519: 类、结构或接口成员声明中的标记“:”无效
btn1.Attributes.Add("onclick", "document.getElementById('"+btn1.ClientID+"').click();");
((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('"+((LinkButton)e.Row.Cells[8].FindControl("btnl")).ClientID+"').click();");
//这样写看看
{
GridViewRow gvr = (sender as LinkButton).NamingContainer as GridViewRow;
LinkButton LinkButton1 = sender as LinkButton ;
}
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("test");
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr[0] = "测试";
dt.Rows.Add(dr);
}
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button button=(Button) e.Row.FindControl("Button1");
button.Attributes.Add("onclick","alert('测试成功!')");
}
}
在header row应该是找不到那个control
脚本已经注册成功了,其他的功能都能实现,就是click事件有问题
{ if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
LinkButton btn1 = ((LinkButton)e.Row.FindControl("btnl")); btn1.Attributes.Add("onclick", "document.getElementById('" + btn1.ClientID + "').click();");
}
}这样也没用,触发不了后台事件
现在的问题是注册自己的click事件不触发后台事件,注册其他button的click能触发后台事件,但页面不显示效果
{ if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('btnl').click();"); }
}
直接双击 Button不就进入它的后台事件了吗
如果给A注册B的click事件,能执行后台B的click事件,但页面没效果。讲的清楚了吧
你看我12楼的代码,就是你的建议,不过一样不能触发后台click事件
我得说一下,按钮在gridview中,不是随便一个按钮
((LinkButton)e.Row.Cells[8].FindControl("btnl")).Attributes.Add("onclick", "document.getElementById('btnl').click();");这句话,好像无意义,给LinkButton加了个onclick事件,来执行自己的click(),可不就是直接双击LinkButton实现的方法吗
另外一个,document.getElementById('btnl')得不到你想要的LinkButton,因为btn1是给每一行加的,在客户端,不存在这个btn1,会自动给每一行的这个按钮生成一个ClientID,如果想FindControl
应该linkButton lbtn1=e.Row.Cells[8].FindControl("btnl") as LinkButton;
lbtn1.Attributes["onclick"]=string.Format("document.getElementById('{0}').click()",lbtn1.ClientID)
前台代码 <asp:GridView ID="GridView1" ShowFooter="false" BorderColor="Black" OnRowDataBound="GridView1_RowDataBound" runat="server" AutoGenerateColumns="False" Font-Size="12px" Width="549px" AllowPaging="True" AutoBack="true"> <Columns>
<asp:BoundField DataField="ID" HeaderText="编号" />
<asp:BoundField DataField="EmpID" HeaderText="账号" />
<asp:BoundField DataField="EmpRealName" HeaderText="姓名" />
<asp:BoundField DataField="EmpSex" HeaderText="性别" />
<asp:BoundField DataField="EmpAddress" HeaderText="住址" />
<asp:BoundField DataField="EmpZipCode" HeaderText="邮编" />
<asp:BoundField DataField="EmpBirthday" HeaderText="生日" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False" />
<asp:BoundField DataField="EmpSalary" HeaderText="薪水" DataFormatString="{0:c}" HtmlEncode="False" />
<asp:TemplateField>
<ItemTemplate >
<asp:LinkButton ID="btnl" runat="server" Text="test"></asp:LinkButton>
</ItemTemplate> </asp:TemplateField>
</Columns>
<HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<PagerStyle HorizontalAlign="Center" BackColor="#FFFFCC" BorderStyle="None" BorderWidth="0px" ForeColor="#330099" />
<PagerSettings Visible="False" />
</asp:GridView>能双击吗??
<ItemTemplate >
<asp:LinkButton ID="btnl" runat="server" Text="test"></asp:LinkButton>
</ItemTemplate>
这个不是一个普通的LinkButton?
点击他想做什么操作,双击去实现他的动作有什么问题
右击GridView,有个编辑模板列,就看到那个LinkButton了,可以双击了吧