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)
{
}
}