是否有Panel
或任何带有CommandName
的容器,CommandArguments
而不是使用按钮(LinkButton,ImageButton,...)?
我想在GridView中添加一列来选择行,整个单元格的矩形而不是Select链接。
答案 0 :(得分:1)
通过实现CommandName
接口,您可以(几乎)实现CommandArguments
和IButtonControl
的任何实现。您可能还希望实现IPostBackEventHandler
接口。
This article详细详细说明了您的要求,通常是:在命令控件中创建Panel
。这并非完全无足轻重。
但是,使表格行可点击更容易,尤其是。使用jQuery。请参阅this article.您只需将事件绑定到该行,然后从那里开始。在此示例中,他们将重定向到URL,以获取click事件中行中的链接。您可以轻松地执行其他操作,例如调用__doPostBack
来导致异步回发并运行任意服务器代码,例如。
答案 1 :(得分:0)
以下是另一种如何将GridView-Cell作为Sort-Column的方法(参见RowCreated,其余的是样本数据):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindGridView()
End If
End Sub
Private Sub BindGridView()
Dim source As New List(Of String)(New String() {"1. row", "2. row", "3. row", "4. row", "5. row"})
Me.GridView1.DataSource = source
Me.GridView1.DataBind()
End Sub
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim cell As New TableCell
If e.Row.RowType = DataControlRowType.DataRow Then
cell.Text = "select"
cell.ToolTip = "click to select row"
cell.Attributes("onmouseover") = "this.style.cursor='pointer';"
cell.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(DirectCast(sender, GridView), "Select$" & e.Row.RowIndex)
End If
e.Row.Cells.Add(cell)
End Sub
编辑:如果从ASP.Net获得“无效的回发或回调参数”-Exception,您可以设置EnableEventValidation="False"
或在页面的代码隐藏中添加以下内容:
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
ClientScript.RegisterForEventValidation(GridView1.UniqueID, "Select$" & row.RowIndex)
End If
Next
MyBase.Render(writer)
End Sub
如果您需要C#,请将所有内容复制到:http://www.developerfusion.com/tools/convert/vb-to-csharp/