Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gridview with multiple buttons

Status
Not open for further replies.

un33k

Programmer
May 8, 2008
1
hi i have a big problem which im sure will be simple to some people. ok i have a gridview with a hyperlink and two buttons in 3 different columns. im generating values for these gridview fields and i need to know if multiple buttons are used in a GridView control how can i determine which button was clicked.
this code below
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
this calls the same function when i click on any button and i don't want this because i have 2 other buttons which i want to use for other functions. how can i refer to each button.
any sample code would be much appreciated.
 
You can get that information from sender.
Something like this:
Button btn = (Button)sender;
if (btn.Name=="B1")
// Do this
elseif(btn.Name=="B2")
// Do this ... an so on
else
// Else part

Try it out...This may help you.

Sharing the best from my side...

--Prashant--
 
set the commandname and commandargument properties for each button within the grid. define the gridviewrowcommand event.
Code:
<asp:GridView onGridViewRowCommand="FireCommand">
   <Columns>
      <TemplateColumn>
          <asp:Button commandname="Foo" CommandArugment='<%Eval("This")#%>' />
          <asp:Button commandname="Bar" CommandArugment='<%Eval("That")#%>' />
      </TemplateColumn>
   </Columns>
</asp:GridView>
Code:
protected void FireCommand(object sender, GridViewRowCommandEventArgs e)
{
     switch (e.CommandName)
     {
          case "Foo":
             int i = (int)e.CommandArgument;
             DoSomethingWith(i);
            break;
          case "Bar":
             DateTime date = (DateTime)e.CommandArgument;
             DoSomethingelseWith(date);
            break;
     }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top