I am using UltraWebGrid in my project. The requirement is that I need to attach a function for onclick event to only those cells which have a value in it and this can be done only from code-behind. But I'm unable to find a solution for this. I tried finding "Attributes.Add" for a cell from code-behind to attach a function on "onclick" event of the cell but there's no "Attributes" property for a cell.
Please suggest how can I attach the "onclick" event to only the desired cell through code-behind in a WebGrid.
RegardsAnuj
Ah, I see now. Thanks for the clarification.
Use the grid's client-side CellClick event, which gives you the ID of the cell that was clicked. As I'd previously suggested for the ClickCellButton event, you can then selectively determine whether or not to take action.
This client-side event applies to the whole grid, not to individual cells. As a result, you don't wire it up cell-by-cell like you've described with an ASP.NET GridView. If you establish this event conditionally, you'll need to be able to evaluate that condition in JavaScript. If the comparison needs to be done on the server, you might consider adding a hidden column whose value you set on the server, and use the value of that column as part of your condition.
Another, slightly different approach would be to put the HTML of an anchor tag as the Value of your cell, in which you call the JavaScript method you want to call. The difference in behavior is that it would require the user to click specifically on the link, rather than anywhere in the cell. Extending your example:
e.Row.Cells(Index-1).Value = "<a href='BLOCKED SCRIPTShowDataValue(" + status + ")>Some Text Here</a>"
Vince, actually I am not using the Button type grid column. In this case it is a normal text column and has no special attributes. What I have been trying to do is capture the Cell click event at the client side. My only question now is - Is This Possible. If YES, then can you please share the code for this.
For reference you can see what I have been trying to do in the code snippet that I posted earlier.
techieanuj said:Now can you please suggest some workaround which can help me in achieving the same functionality desired as explained above?
Private Sub UWGIndicatorCountryView_InitializeRow(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebGrid.RowEventArgs) Handles UWGIndicatorCountryView.InitializeRow For Index As Integer = 1 To Me.UWGIndicatorCountryView.Bands(0).Columns.Count - 1 If Index Mod 3 = 0 Then If Not String.IsNullOrEmpty(e.Row.Cells(Index).Text) Then Dim status As String = e.Row.Cells(Index).Text Dim color As String = GetLegendColor(status) Here's the condition based on which I'm setting the color of the cell and here I want to attach the "onclick" event on this particular cell e.Row.Cells(Index - 1).Style.BackColor = System.Drawing.ColorTranslator.FromHtml(color) Now if I would've been using ASP.Net Gridview then I would've been able to attach "onclick" event to the cell as follows: e.Row.Cells(Index-1).Attributes.Add("onclick","ShowDataValue('" & status & "');") But here I'm using Infragistics WebGrid and it doesn't expose Attributes property for a cell. Now can you please suggest some workaround which can help me in achieving the same functionality desired as explained above? End If End If Next If e.Row.Band.Index = 1 Then For Index As Integer = 4 To Me.UWGIndicatorCountryView.Bands(1).Columns.Count - 1 Step 3 If Not String.IsNullOrEmpty(e.Row.Cells(Index).Text) Then Dim status As String = e.Row.Cells(Index).Text Dim color As String = GetLegendColor(status) e.Row.Cells(Index - 1).Style.BackColor = System.Drawing.ColorTranslator.FromHtml(color) End If Next End If End Sub
Anuj,
I assume that you're using a grid column whose Type is set to Button.
You have two options here that I can see.
One option is to use a templated column with buttons, rather than using a column with its Type set to Button. You can then establish client-side event handlers to each button independently, or not establish them at all, as you choose. This approach gives you the most control, at the cost of a significant amount of work to code and a likely decrease in performance (compared to not using a templated column). If you're already using a templated column, then this is the approach I would recommend.
The other option is to add logic to the ClickCellButton client-side event handler to selectively take action based on conditions with respect to the cell whose button was clicked. For instance, you can choose to take action when another cell in the same row as the one whose button was clicked contains a value. In this manner, the ClickCellButton event is still raised for all buttons, yet your logic still occurs only when it needs to occur. If you're not using a templated column, then this is the approach I most recommend.