Solved

Get the DataKey values in GridView RowCommand event

Posted on 2010-08-15
2
5,243 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
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now