Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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


and this is my C# code:

C#
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
Comments
[no name] 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 ?
[no name] 17-Feb-13 6:16am    
try the solution i posted

First of all, remove following lines from your code

C#
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.NET
<asp:gridview id="grdAppuser" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown">
OnRowCommand="grdAppuser_RowCommand" OnRowDataBound="grdAppuser_RowDataBound" Width="90%">


<asp:imagebutton id="imgbtnDelete" commandname="Delete1" commandargument="<%#Eval("VisID")%>" runat="server" imageurl="images/button_delete.png" />
</asp:gridview>



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


C#
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.
 
Share this answer
 
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
If you just need to fire ImageButton event then register ImageButton event you can get value there.
 
Share this answer
 

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900