在GridView中添加<asp:CommandField ShowSelectButton="True" />,点击选择按钮可选择一行。此时,GridView.SelectedRow.RowIndex等于对应选中的行。
我想通过单击行来实现这一效果,而不添加SelectButton。这样我就可以通过GridView.SelectedRow.RowIndex来获取我要的那一行数据。 请问如何实现?谢谢! 我看页面生成的javascript,在后台中添加如下代码,可实现,不过似乎不好,要将页面设置为 enableEventValidation="false" :
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("style", "cursor:hand;");
e.Row.Attributes.Add("onclick", string.Format("__doPostBack('GridView', 'Select${0}');", e.Row.DataItemIndex));
}
}
我想通过单击行来实现这一效果,而不添加SelectButton。这样我就可以通过GridView.SelectedRow.RowIndex来获取我要的那一行数据。 请问如何实现?谢谢! 我看页面生成的javascript,在后台中添加如下代码,可实现,不过似乎不好,要将页面设置为 enableEventValidation="false" :
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("style", "cursor:hand;");
e.Row.Attributes.Add("onclick", string.Format("__doPostBack('GridView', 'Select${0}');", e.Row.DataItemIndex));
}
}
解决方案 »
- 如何用asp.net(C#)读asp写的cookies值?
- 请问个页面设置的问题!
- 关于海量数据的快速检索问题
- 如何调用VB6写的DLL(回者有分!!!在线急等!!!)
- ORA-01036:非法的变量名/编号
- 如何设置dataGrid列的宽度,使其里面的内容过多时自动换行?谢谢了^_^
- 如何在DataGrid每条记录下面画一条线
- 用oledb更新excel的数据
- 请问大家在asp.net应用中如何做权限控制?
- 求助 网页怎么通过香港服务器将谷歌搜索到的内容显现到自己的页面??急
- 突发问题 粘贴过来的DataGrid 事件不响应了
- 如何GridView中单击一行等效于触发SelectButton事件
应该放在这里面
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "highlight(this);");
}
}
function highlight(o){
e = o;
o = o.parentElement;
while(o!=null && o.tagName != "TABLE"){
o = o.parentElement;
}
for(var i =0;i<o.rows.length;i++){
o.rows[i].style.backgroundColor = "white";
}
e.style.backgroundColor = "red";
}
{
GridViewRow row = CustomersGridView.SelectedRow;
Message.Text = "You selected " + row.Cells[2].Text + ".";
} protected void CustomersGridView_RowDataBound( object sender, GridViewRowEventArgs e )
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Attributes.Add("style", "display:none");
e.Row.Attributes.Add("onclick", "this.cells[0].childNodes[0].click()");
}
}
</script><html>
<body>
<form id="Form1" runat="server">
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="True"
selectedindex="0"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server" OnRowDataBound="CustomersGridView_RowDataBound">
<selectedrowstyle backcolor="LightCyan" forecolor="DarkBlue"
font-bold="True"/>
<Columns>
<asp:CommandField ShowSelectButton="True"/>
</Columns>
</asp:gridview>
<br/>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<asp:sqldatasource id="CustomersSource"
selectcommand="SELECT [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] FROM [Customers]"
connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
runat="server"/>
</form>
</body>
</html>
private void grdData2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType==ListItemType.EditItem)
{
e.Item.Attributes.Add("onmouseover", "javascript: TRBackColor = this.style.backgroundColor; this.style.backgroundColor = TRMoveColor;");
e.Item.Attributes.Add("onmouseout", "javascript: this.style.backgroundColor = TRBackColor;");
e.Item.Style.Add("cursor", "mouse");
e.Item.Attributes.Add
("onclick", "javascript: 某个javascript函数();");
}
}
<script runat="server">
void CustomersGridView_SelectedIndexChanged( Object sender, EventArgs e )
{
GridViewRow row = CustomersGridView.SelectedRow;
Message.Text = "您选择的是:" + row.Cells[2].Text + ".";
}
protected void CustomersGridView_RowDataBound( object sender, GridViewRowEventArgs e )
{
e.Row.Cells[0].Attributes.Add("style", "display:none");
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "this.cells[0].childNodes[0].click()");
}
}
protected void Page_Load( object sender, EventArgs e )
{
CustomersGridView.HeaderRow.Cells[0].Attributes.Add("style", "display:none");
CustomersGridView.FooterRow.Cells[0].Attributes.Add("style", "display:none");
}
</script>
<html>
<body>
<form id="Form1" runat="server">
<asp:GridView ID="CustomersGridView" DataSourceID="CustomersSource" AllowPaging="True"
SelectedIndex="0" OnSelectedIndexChanged="CustomersGridView_SelectedIndexChanged"
runat="server" OnRowDataBound="CustomersGridView_RowDataBound">
<SelectedRowStyle BackColor="LightCyan" ForeColor="DarkBlue" Font-Bold="True" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<br />
<asp:Label ID="Message" ForeColor="Red" runat="server" />
<asp:SqlDataSource ID="CustomersSource" SelectCommand="SELECT [CustomerID], [CompanyName], [Address] FROM [Customers]"
ConnectionString="Server=(local);Database=Northwind;Trusted_Connection=True;" runat="server" />
</form>
</body>
</html>
我就是不知道怎么通过客户端脚本触发这个事件。