思路就是点击时通过__doPostBack执行服务器事件。前台代码
<asp:TemplateField><ItemTemplate>
<asp:Button
ID="btnShowDetail"
runat="server"
Text='<%# Eval("fName") %>'
CssClass="btnNoBorder"
OnClick="btnShowDetail_Click">
</asp:Button>
</ItemTemplate></asp:TemplateField>后台
protected void gvFriendList_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (gvr.RowType == DataControlRowType.DataRow)
{
Button btn = (Button)gvr.FindControl("btnShowDetail");
gvr.Attributes.Add("onclick","__doPostBack('" + btn.ClientID + "','');");
}
}
}这是FF中查看生成的代码
<tr onclick="__doPostBack('gvFriendList_ctl03_btnShowDetail','');">
<td>
<input id="gvFriendList_ctl03_ckBox" type="checkbox" name="gvFriendList$ctl03$ckBox" />
</td><td> <input type="submit" name="gvFriendList$ctl03$btnShowDetail" value="朋友" id="gvFriendList_ctl03_btnShowDetail" class="btnNoBorder" />
</td>
</tr>
为什么点击没效果呢
<asp:TemplateField><ItemTemplate>
<asp:Button
ID="btnShowDetail"
runat="server"
Text='<%# Eval("fName") %>'
CssClass="btnNoBorder"
OnClick="btnShowDetail_Click">
</asp:Button>
</ItemTemplate></asp:TemplateField>后台
protected void gvFriendList_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (gvr.RowType == DataControlRowType.DataRow)
{
Button btn = (Button)gvr.FindControl("btnShowDetail");
gvr.Attributes.Add("onclick","__doPostBack('" + btn.ClientID + "','');");
}
}
}这是FF中查看生成的代码
<tr onclick="__doPostBack('gvFriendList_ctl03_btnShowDetail','');">
<td>
<input id="gvFriendList_ctl03_ckBox" type="checkbox" name="gvFriendList$ctl03$ckBox" />
</td><td> <input type="submit" name="gvFriendList$ctl03$btnShowDetail" value="朋友" id="gvFriendList_ctl03_btnShowDetail" class="btnNoBorder" />
</td>
</tr>
为什么点击没效果呢
if (Membership.ValidateUser(name, psw))
{
//取得 登录用户的唯一ID
ForMoreApplication.LoginUserId = ForMoreApplication.GetPageUserId(name);ForMoreApplication.LoginUserId是个全局静态的变量,在这初始化后,做为条件来取数据库中的数据 。在哪设置一下可以让有效时间长点,我现在数据一会就取不到了……还得重新登录
<asp:TemplateField><ItemTemplate>
<asp:Button
ID="btnShowDetail"
runat="server"
Text='<%# Eval("fName") %>'
CssClass="btnNoBorder"
CommandName="save">
</asp:Button>
</ItemTemplate></asp:TemplateField>然后在gridview的RowCommand这样去判断是哪个按钮点击的
if(e.CommondName == "save")
-------------------------------
你这个是ForMoreApplication.LoginUserId是写在哪里的,你直接保存到session中好点。session["LoginUserId"] = ForMoreApplication.GetPageUserId(name);
取id:
string strId = session["LoginUserId"];
<asp:Button
ID="btnShowDetail"
runat="server"
Text='<%# Eval("fName") %>'
CssClass="btnNoBorder"
CommandName="ShowDetail">
</asp:Button>
</ItemTemplate>-------------------------------
protected void gvFriendList_RowCommand(object sender, GridViewCommandEventArgs e)
{
fName.Text = e.CommandName;
}
-----------------
但单击还是没效果
HTML:
<Script language=javascript>
function linksort(cid)
{
//店面编号
if(cid=="linkbtn")
{
__doPostBack("LinkButton1","LinkButton1_Click");
}
}
</script><html>
...
<asp:BoundColumn DataField="LinkCount" HeaderText="...onClick='linksort(this.id)'">
</html>------------------------------------------------------------------
后台C#在private void LinkButton1_Click(object sender, System.EventArgs e){}
事件里你想怎么写就怎么写吧
我的Button还能触发gvFriendList_RowCommand事件,现在是什么也没有了
长期郁闷中
要用CheckBox就肯定不能了
你要用HTML控件调用后台C#事件的话 肯定要用__doPostBack()
说白了 要是HTML控件直接调用后台web控件事件是不可能的 除非借助一下想LinkButton这样的控件(等于就是当你点击了HTML控件的同时也点击了LinkButton控件,同时也调用了写在C#中LinkButton的事件)。。
就是这么回事,毕竟你的HTML控件不是放在里模板里了吗?
虽然你HTML控件在模板中 但不能用RowCommand事件因为RowCommand事件只有Button会触发假如你模板列中放了CheckBox,RadioButton类似的控件(就算你把AutoPostBack改成了true) 也不会触发RowCommand事件,还照样要在:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{ }
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{ }
这两个事件中写 ,也不能在RowCommand事件中写
到底那些控件会触发RowCommand事件
<td>
<input id="gvFriendList_ctl03_ckBox" type="checkbox" name="gvFriendList$ctl03$ckBox" />
</td><td><input type="submit" name="gvFriendList$ctl03$btnShowDetail" value="朋友" id="gvFriendList_ctl03_btnShowDetail" class="btnNoBorder" />
</td>
</tr>
你说这个为什么不能触发呢,我已经有onclick="__doPostBack('gvFriendList_ctl03_btnShowDetail','');-----------
Button也试了 LinkButton也试了,但问题还是没解决……
tr里也有 onClick事件?
你调用后台的哪个事件 就应该在哪个web控件的Click事件中写,反正以前我多是这么做的
没出过问题,用的全是LinkButton 也是在LinkButton1_Click事件中写的