开发者

How to get Command Functionality from a Panel

Is there a Panel or any container with CommandName, CommandArguments instead of using Buttons (LinkButton, ImageButton, ...)?

I want to add a column in a GridView to make sele开发者_Python百科ction for the row, the whole cell's rectangle instead of a Select link.


You can make (almost) anything implement CommandName and CommandArguments by implementing the IButtonControl interface. You will probably also want to implement the IPostBackEventHandler interface.

This article covers in detail exactly what you are asking about, generally: making a Panel into a command control. It's not entirely trivial.

But making a table row clickable is a lot easier, though, esp. using jQuery. See this article. You just bind an event to the row, and go from there. In this example, they're redirecting to the url for a link in the row on a click event. You could just as easily do something else like call __doPostBack to cause an async postback and run arbitrary server code, e.g.


Here is another way how you can make a GridView-Cell as Sort-Column(see in RowCreated, the rest is sample-data):

    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

Edit: if you get a "Invalid postback or callback argument"-Exception from ASP.Net, you can either set EnableEventValidation="False" or add following to your page's codebehind:

   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

If you need C#, copy all into: http://www.developerfusion.com/tools/convert/vb-to-csharp/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜