相关疑难解决方法(0)

如何在没有选择按钮的情况下在GridView中实现完整行选择?

我正在实现一个功能,当用户按下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的页面(在每个回发).

难道我做错了什么?有更好的实施吗? …

c# asp.net gridview selectedindexchanged

30
推荐指数
2
解决办法
9万
查看次数

将链接列添加到ASP.NET GridView

我想输出可点击的新闻标题列表.到目前为止,我可以打印出标题列表,因为我在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)

asp.net gridview hyperlink

20
推荐指数
2
解决办法
7万
查看次数

标签 统计

asp.net ×2

gridview ×2

c# ×1

hyperlink ×1

selectedindexchanged ×1