0

I have a gridview with 10+ columns and any number of rows based on the date being selected. The gridview looks like this: enter image description here

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:

enter image description here

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:

enter image description here

I can tell you this much though:
The following columns are always existent in the gridview:

  1. In
  2. Out
  3. Load #
  4. Vendor Name
  5. Weight Ticket
  6. Vendor BOL
  7. Truck Gross
  8. Product Net

These columns are standard and in this order.

2 Answers 2

2

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].Textis 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>
Sign up to request clarification or add additional context in comments.

9 Comments

Yeah, I had the syntax right, I just did not post the exact line I used in my program. I was trying to give an idea of a pseudocode I used. But regarding the redirect function being a javascript, does the javascript function go in the aspx file? Or in its own .js file in the scripts folder?
@CrazyCucumber redirectFunction is in aspx markup.
@CrazyCucumber You can call this function in aspx or js file.
I need to call the redirect function with a parameter. The parameter is the value of the cell you just clicked on. How do I do that?
@CrazyCucumber I edited answer. Please see this again.
|
0

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...

CellClick: enter image description here

5 Comments

Will this function behave like a listener? Or do I need to call it somewhere for it to detect any clicks? I am sorry, I am new to this.
No, only click to DataGridView in Designer, in properties click "Events" button, find "CellClick" event and double click a field next to it, it will create a new function like the one above itself and it will work on click into the datagridview. Or copy the code above and make sure that you replace "dgwList after the "Handler" keyword with the name of your DataGridView.
Sorry, "Handler" keyword is in VB.NET only, better use the "Events" in Visual Studio Properties.
A bad day of mine, I missed that ASP.NET tag... I guess it would be different for a web app GridView
Yeah I think so too. I updated my question to show what my gridview looks like, if you could take a look at it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.