Crimius
Crimius

Reputation: 534

Context aware button in a listview

I have a Listview that pulls and displays some data. Field 1 is the PK, Field 2 is a type identifier, and the rest are plain data. I have a button I've made that appears as the last field on every record. The button needs to be able to call the proper page based on Field 2, and pass it the value of Field 1.

How can I tell which record is being clicked, and grab the appropriate values for my logic?

|Field 1|Field 2|Button |
|      1|      2| Click |
|      2|      2| Click |
|      3|      1| Click |

Upvotes: 0

Views: 76

Answers (1)

Icarus
Icarus

Reputation: 63962

In the markup of your ListView where you define your button, do this:

<asp:button id="yourButton" OnClientClick='<%#redirectToProperPage(Eval("Field1"),Eval("Field2"))%>' />

Now, define a JS function like so:

function redirectToProperPage(id1,id2)
 {
      if(id2==1)
      {
        if(id1==1)
           window.location='MyOtherpage.aspx';
        else 
          window.location='Different.aspx';
      }
      else ...
 }

You get the idea... you don't need code behind for this unless you want to do something more than simply redirecting to a different page.

Upvotes: 1

Related Questions