asp.net GridView中,如何选中一行? 需求是这样的,有一个gridview,全部是boundfield绑定的数据源,没有任何模板列。 就单单的一个gridview。 现在就是鼠标单击任何一行,可以获取这行的某个字段或者整个行的信息。请各位大哥大姐们,帮忙指点一下啊,3Q you! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <asp:TemplateField HeaderText="组名称"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("col0") %>' ></asp:Label> <asp:LinkButton ID="a" runat="server" CommandName="b"></asp:LinkButton> </ItemTemplate> </asp:TemplateField>protected void gvColorGroup_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton lbtn = (LinkButton)e.Row.FindControl("a"); e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(lbtn, "")); e.Row.Attributes.Add("style", "cursor:pointer"); } } protected void gvColorGroup_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow curgvr = (GridViewRow)((LinkButton)e.CommandSource).Parent.Parent; Label lbl = (Label)curgvr.FindControl("Label1"); Response.Write(lbl.Text); if (e.CommandName=="b") { FillColor(int.Parse(lbl.Text)); } }模板列隐藏就行了, protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { int i; for (i = 0; i <= GridView1.Rows.Count; i++) { //首先判断是否是数据行 if (e.Row.RowType == DataControlRowType.DataRow) { //当鼠标停留时更改背景色 e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'"); //当鼠标移开时还原背景色 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); //单击行的任意列会自动选中此行 e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')"); } } <asp:GridView ID="GridView5" runat="server" AutoGenerateColumns="False" onrowdatabound="GridView5_RowDataBound" > <Columns> <asp:TemplateField> <ItemTemplate> <%#DataBinder.Eval(Container,"DataItem.ID") %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>protected void GridView5_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("style", "cursor:pointer"); e.Row.Attributes.Add("onclick", "document.getElementById('TextBox5').value=" + e.Row.RowIndex + ""); } } 用惯了repeater ,学习下gridview 谢谢哦,理解了你的代码,我修改了一下哦,3Q you !!!! protected void GridView5_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("style", "cursor:pointer"); e.Row.Attributes.Add("onclick", "document.getElementById('TextBox5').value=" + e.Row.RowIndex + ""); } }把e.Row.RowIndex改成e.Row.Cell["要获取某一列的字段"].Text;就OK了! 请问__doPostBack这个函数应该怎么定义啦,我在后面定义的为什么不行求指点 请教一个mvc自动将文章编码的问题。 关于在线测试的方案 网页上直播视频请教?? javascript代码,替换回车符的问题,急等? 本地运行正常,上传到上传到虚拟主机上就报错,高手指点,重谢! asp.net中自定义控件属性的问题。。。。 谁能提供有关使用vs.net制作安装包的相关资料 Access 求一句SQL语句怎么写 关于超级字符串问题 一个超级郁闷的问题。 flash实现自动循环播放 本地vs运行没有问题,发布到IIS上就出现问题,在线等啊!!
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("col0") %>' ></asp:Label>
<asp:LinkButton ID="a" runat="server" CommandName="b"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void gvColorGroup_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbtn = (LinkButton)e.Row.FindControl("a");
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(lbtn, ""));
e.Row.Attributes.Add("style", "cursor:pointer");
}
}
protected void gvColorGroup_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow curgvr = (GridViewRow)((LinkButton)e.CommandSource).Parent.Parent;
Label lbl = (Label)curgvr.FindControl("Label1");
Response.Write(lbl.Text);
if (e.CommandName=="b")
{
FillColor(int.Parse(lbl.Text));
}
}模板列隐藏就行了,
{
int i;
for (i = 0; i <= GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//单击行的任意列会自动选中此行
e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");
}
}
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#DataBinder.Eval(Container,"DataItem.ID") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>protected void GridView5_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("style", "cursor:pointer");
e.Row.Attributes.Add("onclick", "document.getElementById('TextBox5').value=" + e.Row.RowIndex + "");
}
}
谢谢哦,理解了你的代码,我修改了一下哦,3Q you !!!!
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("style", "cursor:pointer");
e.Row.Attributes.Add("onclick", "document.getElementById('TextBox5').value=" + e.Row.RowIndex + "");
}
}
把e.Row.RowIndex改成e.Row.Cell["要获取某一列的字段"].Text;就OK了!