Solved

Get the DataKey values in GridView RowCommand event

Posted on 2010-08-15
2
5,329 Views
Last Modified: 2012-05-10
Hi!

I have a GridView with a Delete button (not default delete button, I'm using command name).

GridView_RowCommand {

if (e.CommandName.CompareTo("apagarNoticia") == 0)    {
newsid = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["NewsID"].ToString();
lang = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["LanguageID"].ToString();
}

Open in new window


<asp:Button ID="ButtonApagarNoticia" runat="server"
CausesValidation="false" CommandName="apagarNoticia" Text="Apagar" />

Open in new window


This delete button has a javascript confirmation.

        protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button b = (Button)e.Row.FindControl("ButtonApagarNoticia");
                b.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete the news " + DataBinder.Eval(e.Row.DataItem, "NewsID") + " ?')");
            }
        }

Open in new window

Everything worked well until I changed the delete button (not the default, I'm using command name) to template and add javascript confirmation.

Error Input string was not in a correct format on this line:
newsid = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["NewsID"].ToString();

In other pages, I just used the CommandArgument
<asp:Button ID="ButtonDeleteSim" runat="server"
CausesValidation="false" CommandName="apagarSimulacao"
CommandArgument='<%# Bind("NewsID") %>' Text="Apagar" />

Open in new window


string newsid = e.CommandArgument.ToString();

Open in new window


But I need two fields this time.

newsid = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["NewsID"].ToString();
lang = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["LanguageID"].ToString();

Open in new window


How can I solve this?

Thanks in advance!
0
Comment
Question by:calypsoworld
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 500 total points
ID: 33443550
try the following code....

protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer && e.Row.RowType !=DataControlRowType.Pager)
      {
            Button btnDeleteSim =new Button();
            btnDeleteSim = (Button)e.Row.FindControl("ButtonDeleteSim");
            btnDeleteSim.CommandArgument= e.Row.RowIndex.ToString();
      }
}

Then get the row index in

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{

      int RowIndex_ = int.Parse(e.CommandArgument.ToString());
      newsid = GridView3.DataKeys[RowIndex_]["NewsID"].ToString();
      lang = GridView3.DataKeys[RowIndex_]["LanguageID"].ToString();

}
0
 

Author Closing Comment

by:calypsoworld
ID: 33444715
Awesome!
Thank you so much!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LINQ Select all records by MAX data syntax help 1 28
Testing of the web services 1 48
Where is this file? 3 33
VB.NET - Refactor Class per SOLID principles 2 33
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question