Reputation: 479
I have a gridview
with 10+ columns and any number of rows based on the date being selected. The gridview
looks like this:
There are several parameters that can be selected that generates this gridview. I am trying to implement an onClick
event on the Weight Ticket
column. I want to be able to click on individual weight tickets and the page will redirect to a different page that shows that weight ticket's details.
I've tried implementing the following:
e.Row.cells[4].Attribute["onClick"] = redirectFunction
That did not do anything. I am unable to click on the weight ticket.
I would really appreciate any help regarding this.
Optional requirement
This is not asked of me, but I'd like to be able to add something else to this. The redirect is done like this:
Response.Redirect("~/pages/TicketDetails.aspx?wt=" + weightTicket + "&act=" + actionMethod +"&src=" + HttpContext.Current.Request.Url);
The actionMethod
is what determines whether the TicketDetails
page allows editing. If the actionMethod
is set to View
, you cannot edit it. If it is set to Edit
, you can. I want to be able to dynamically tell the redirect function that it is a view action or edit action.
It is possible if you can look at the In
, Out
, or Load #
columns. If any one of them have NA
, then it is an edit
action. If they have a value that is not NA, then it is a view. This is just an additional request. I can always just open it as edit and not worry about all this, but it would be helpful if I could do this too.
Any help would be appreciated.
Thank you
Edit 1:
The line I've written in the question (my attempt) is just a pseudo-code. Here is the actual line in my program:
e.Row.Cells[4].Attributes["onClick"] = "dataCellClick(" + e.Row.Cells[4].ToString() + ", view;";
dataCellClick()
is the function I am calling which in turn does a redirect. I've tried not calling a function but calling the redirect function right here and that didn't work. So I tried this. The two parameters to the function call is WeightTicket
and actionMethod
.
Edit 2:
The gridview that is already in place is dynamic. By that, I mean:
This is what my designer view looks like for the page. The gridview is selected in the image. It does not have all the columns listed out one by one. It gets the columns AND the rows dynamically from a query.
So, these are all the event handler properties I have:
I can tell you this much though:
The following columns are always existent in the gridview:
These columns are standard and in this order.
Upvotes: 0
Views: 1296
Reputation: 9927
This portion of your code isn't correct:
e.Row.cells[4].Attribute["onClick"] = redirectFunction
You should edited like this in RowDataBound
(I supposed e.Row.Cells[4].Text
is page name) :
e.Row.Cells[4].Attributes["onClick"] = "redirectFunction('"+e.Row.Cells[4].Text+"')";
redirectFunction
is JavaScript function like this (redirect to pageName.aspx
):
<script>
function redirectFunction(pageName) {
window.location.href = pageName + ".aspx";
}
</script>
Upvotes: 2
Reputation: 2000
I'd suggest to use CellClick event and check, whether a column index clicked is the one of the "Weight Ticket" (this is VB.NET):
Private Sub dgwList_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgwList.CellClick
If e.ColumnIndex = 4 Then
Call MyRedirectFunciton()
End If
End Sub
in C# it should be something like this:
private void dgwList_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4) {
MyRedirectFunciton();
}
}
Of course, you can pass a RowIndex too...
Upvotes: 0