i'm trying to hide EDIT button in a column when i click it but it doesn't. Actually, i manually created EDIT button in gridview, it worked :) but now after clicking edit button i want it to disable so i can put UPDATE button while editing.
i tried but it doesn't.

Code:
.aspx

<asp:GridView ID="gvTest" runat= "server" OnRowDataBound="gvTest_RowDataBound" OnRowEditing="gvTest_Edit" DataKeyNames="stid">
      <Columns>
       <asp:TemplateField HeaderText="Delete" >
        <ItemTemplate>
         <asp:Button ID="btnDelete" runat="server"  Text="Delete" onCommand="gvTest_Delete"  CommandName="deleterow" 
          CommandArgument='<%# Container.DataItemIndex %>'   />
        <asp:Button ID= "btnEdit" runat="server" Text="Edit" CommandName="edit"  OnCommand="gvTest_ButtonChange" 
          CommandArgument='<%# Container.DataItemIndex %>'/>
          .
          .
          .
          .
          .
          .
          .
          .



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    public String constrng = "Data Source=PALERIDER-PC;Initial Catalog=Login;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            connectToDb();
        }
    }

    public void connectToDb() 
    {
        SqlConnection sqlcon = new SqlConnection(constrng);
        String com1 = "select * from login";
        SqlCommand sqlcom = new SqlCommand(com1, sqlcon);
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();

        try
        {
            sqlcon.Open();
            sqlda.Fill(ds);
            gvTest.DataSource = ds;
            gvTest.DataBind();
        }
        catch (Exception exc)
        {
            Response.Write(exc.ToString());
        }
        finally
        {
            sqlcon.Close();
        }
    }

    protected void gvTest_RowDataBound(Object sender, GridViewRowEventArgs e) 
    {
        if (e.Row.RowType == DataControlRowType.DataRow) 
        {
            DropDownList ddlist = (DropDownList)e.Row.FindControl("ddListTest");
            ddlist.Items.Insert(0, "Select");
        }
    }

    protected void gvTest_Delete(Object sender, CommandEventArgs e) 
    {
        if (e.CommandName == "deleterow")
        {

            int index_row = Convert.ToInt32(e.CommandArgument);
            Response.Write(index_row+"</br>");

            //  Button GrdBtn = (Button)gvTest.Rows[index_row].FindControl("btnDelete");
          //  TextBox GrdTxtBox = (TextBox)gvTest.Rows[index_row].FindControl("txtboxTest");
          //  DropDownList GrdDlist = (DropDownList)gvTest.Rows[index_row].FindControl("ddListTest");

           // String delete_comm = "delete from login where stid= '"+index_row+"' ";

            int pk = Convert.ToInt32(gvTest.DataKeys[index_row].Value);  

            SqlConnection sqlcon = new SqlConnection(constrng);
            SqlCommand sqlComDelte = new SqlCommand("deleteRow", sqlcon);
            sqlComDelte.CommandType = CommandType.StoredProcedure;

            sqlComDelte.Parameters.Add("@index", SqlDbType.Int).Value = pk ;
            Response.Write("PK=" + pk+"</br>");
            //sqlComDelte.Parameters["@index"].Value = index_row;

            try
            {
                sqlcon.Open();
                int numRowEffected = Convert.ToInt32(sqlComDelte.ExecuteNonQuery());
                Response.Write("Num of rows deleted="+numRowEffected+"</br>");
                connectToDb();
                Response.Write("Loaded again");
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
            finally 
            {
                sqlcon.Close();
            }

        }
    }

    protected void gvTest_Edit(Object sender, GridViewEditEventArgs e) 
    {
        gvTest.EditIndex = e.NewEditIndex;
        Response.Write("Editing...");
        connectToDb();
    }

    protected void gvTest_ButtonChange(Object sender, CommandEventArgs e) 
    {

        int index = Convert.ToInt32 (e.CommandArgument);
        Response.Write("</br>" + "Button event, Index selected="+ index);
        gvTest.Rows[index].FindControl("btnEdit").Visible = false;
        //Response.Write("Now hidden");
    }

    protected void gvTest_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

I think it would be easier to handle the showing/hiding client side, not via aspx code but by using jQuery. It will produce a better client experience with no postback.

yes jorGem, that's fine but i'm doing it on purpose, if there is any way ?

actually i'm trying to get the nature of asp.net programming, so doing stupid stuff just to improve mey server side coding much more then before.
So please

actually i'm trying to get the nature of asp.net programming, so doing stupid stuff just to improve mey server side coding much more then before.

Yes, I know why you are doing this...that's great. But take a look at a typical gridview with editing enabled... The edit, update, cancel buttons work client side.

I'm not recommending to follow that approach because that's how the gridview does it, but because it makes sense.

If a user clicks on edit, update, or cancel, its a better experience for the user because you're not making a round trip back to the server.

I'm sure you can do this server side as you are attempting...before I try to reprodoce something like this, I wanted to mention the client side approach because that's what I would likely do even if i was building this using your approach.

sure JorGem, that sounds great, meanwhile i figured this way out

       <asp:TemplateField HeaderText="Delete" >
        <ItemTemplate>
         <asp:Button ID="btnDelete" runat="server"  Text="Delete" onCommand="gvTest_Delete"  CommandName="deleterow" 
          CommandArgument='<%# Container.DataItemIndex %>'   />
        <asp:Button ID= "btnEdit" runat="server" Text="Edit" CommandName="edit"  OnCommand="gvTest_ButtonChange" 
          CommandArgument='<%# Container.DataItemIndex %>'/>
        </ItemTemplate>

        <EditItemTemplate>
         <asp:Button ID="BtnUpdate" runat="server" CommandName="Update" Text="Update"  />
         <asp:Button ID="BtnCancel" runat="server" CommandName="Cancel" Text="Cancel"  />
        </EditItemTemplate>
       </asp:TemplateField>

Yeah, that makes sense to use the appropriate templates. What are you doing code behind to show the appropriate template? Good job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.