我正在实现一个功能,当用户按下GridView中行中的任何一点时,将选择该行而不是选择按钮.

为了实现这一点,我使用以下代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Set the hand mouse cursor for the selected row.
e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");
// The seelctButton exists for ensuring the selection functionality
// and bind it with the appropriate event hanlder.
LinkButton selectButton = new LinkButton()
{
CommandName = "Select",
Text = e.Row.Cells[0].Text
};
e.Row.Cells[0].Controls.Add(selectButton);
e.Row.Attributes["OnClick"] =
Page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,存在以下问题:
EnableEventValidation将页面设置为时,这才能正常工作false.SelectedIndexChanged仅触发公正的情况下Grid.DataBind()被调用Page_Load的页面(在每个回发).难道我做错了什么?有更好的实施吗? …
我想输出可点击的新闻标题列表.到目前为止,我可以打印出标题列表,因为我在VS 2010的设计器视图中拖放了NewsHeadline表.您认为我应该如何使列表元素可点击?我找了一个URL属性,但我没有看到它.我需要包裹<a href吗?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="NewsHeadline" HeaderText="NewsHeadline"
SortExpression="NewsHeadline" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>"
SelectCommand="SELECT [NewsHeadline] FROM [NewsTable]"></asp:SqlDataSource>
</form>
Run Code Online (Sandbox Code Playgroud)