Click here to Skip to main content
12,073,325 members (77,443 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hello all

i am trying to get ID value from my GridView using CommandArgument, but when it is return the row index value instead of ID value

<asp:TemplateField HeaderText="           " HeaderStyle-HorizontalAlign="Center">
       <ItemTemplate>
                     <asp:ImageButton ID="imgbtnDelete" CommandName="Delete1"                          CommandArgument='<%# Eval("VisID") %>' runat="server" ImageUrl="images/button_delete.png"/>
       </ItemTemplate>

and this is my C# code:

protected void grdAppuser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton delBtn = (ImageButton)e.Row.FindControl("imgbtnDelete");
 
            delBtn.Attributes.Add("OnClick", "return confirm('Are you sure you want to delete');");
 
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdAppuser, "Select$" + e.Row.RowIndex);
        }
 

protected void grdAppuser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
 
        try
        {
            switch (e.CommandName)
            {
                case ("Delete1"):
                    
                    string id = Convert.ToString(e.CommandArgument); // the problem here this line return row index instead of ID
     }

so as i said the e.CommandArgument return row index instead of ID value.

please help

thank you
Posted 16-Feb-13 13:17pm
Medo-I644
Comments
Sheikh Muhammad Haris 16-Feb-13 21:56pm
   
What do you want to do with this line of code ?


e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdAppuser, "Select$" + e.Row.RowIndex);
Medo-I 17-Feb-13 0:21am
   
i am not sure, i found the hole lines of code in this block in the internet, i tried to delete it, the event protected void grdAppuser_RowCommand(object sender, GridViewCommandEventArgs e) stop running
Sandeep Mewara 17-Feb-13 0:59am
   
CommandArgument is set for 'VisID' and will return so. What do you see in Debug ?
Sheikh Muhammad Haris 17-Feb-13 6:16am
   
try the solution i posted
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

First of all, remove following lines from your code

e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdAppuser, "Select$" + e.Row.RowIndex);
 
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

Then Add following events to your gridView aspx

<asp:gridview id="grdAppuser" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown">
OnRowCommand="grdAppuser_RowCommand" OnRowDataBound="grdAppuser_RowDataBound" Width="90%">


Then add this in your Row Command event, you will surely get e.CommandArgument value


protected void grdAppuser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
            {
                case ("Delete1"):                    
                    string id = Convert.ToString(e.CommandArgument); 
            }
     }


Hope this will work for you.
  Permalink  
Comments
Medo-I 17-Feb-13 15:27pm
   
hello

this is my gridview, it is already has OnRowCommand event:

<asp:GridView ID="grdAppuser" runat="server" CssClass="datatable" SelectedIndex="0"
AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" CellPadding="0"
BorderWidth="0px" GridLines="None" DataKeyNames="VisID" ShowFooter="True" OnPageIndexChanging="grdAppuser_PageIndexChanging"
OnSorting="grdAppuser_Sorting" OnRowCreated="grdAppuser_RowCreated" OnRowDataBound="grdAppuser_RowDataBound"
OnSelectedIndexChanged="grdAppuser_SelectedIndexChanged" OnRowCommand="grdAppuser_RowCommand"
OnPreRender="grdAppuser_PreRender" Width="800px" PageSize="30">


any way, i deleted the two lines you mentioned in your answer, and when i press on the imagebutton the RowCommand even did not run.

thank you
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

If you just need to fire ImageButton event then register ImageButton event you can get value there.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100