我想编辑并从网格中删除一行,但是我在选择任何一行时默认选择第一行并编辑它? [英] I Want To Edit And Delete A Row From Grid, Bt Wenever I Am Selecting Any Row It By Default Select The 1St Row And Edits It?
问题描述
private void editdata()
{
试试
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [sqlcon] .ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(TF_editdetails,con);
da.SelectCommand .CommandType = CommandType.StoredProcedure;
con.Open();
da.SelectCommand.Parameters.AddWithValue(@ Name,hfname.Value);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt!= null&& dt.Rows。计数> 0)
{
txtname.Text = dt.Rows [0] [Name]。ToString();
hfname.Value = dt.Rows [0] [Name]。ToString();
txtage.Text = dt.Rows [0] [age]。ToString();
txtcity.Text = dt.Rows [0] [city]。ToString();
txtmobile.Text = dt.Rows [0] [mobile]。ToString();
txtemail.Text = dt.Rows [0] [email]。ToString() ;
txtbxlogin.Text = dt.Rows [0] [login_ID]。ToString();
txtbxpswrd.Text = dt.Rows [0] [password ] .ToString();
}
btnupdate.Visible = true;
}
catch
{
throw;
}
}
private void deletedata()
{
试试
{< br $>
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [sqlcon] .ConnectionString);
SqlCommand cmd = new SqlCommand(TF_deletedetails ,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ Nam e,hfname.Value);
con.Open();
int res = cmd.ExecuteNonQuery();
con.Close ();
if(res> 0)
{
ScriptManager.RegisterStartupScript(this,this.GetType(),Message,alert('DATA DELETED');,true); < br $>
}
}
catch
{
throw;
}
}
在顶部的页面加载中添加这个
if (ispostback)
{
return ;
}
试一试
当你点击网格按钮,因为它是服务器端控件它会创建一个回发,这就是你面对这个原因.......... ....
此链接将帮助您
http://www.c-sharpcorner.com/UploadFile/ 9f0ae2 / gridview的 - 编辑删除和更新功能于ASP-网络
private void editdata()
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("TF_editdetails", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
con.Open();
da.SelectCommand.Parameters.AddWithValue("@Name", hfname.Value);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
txtname.Text = dt.Rows[0]["Name"].ToString();
hfname.Value = dt.Rows[0]["Name"].ToString();
txtage.Text = dt.Rows[0]["age"].ToString();
txtcity.Text = dt.Rows[0]["city"].ToString();
txtmobile.Text = dt.Rows[0]["mobile"].ToString();
txtemail.Text = dt.Rows[0]["email"].ToString();
txtbxlogin.Text = dt.Rows[0]["login_ID"].ToString();
txtbxpswrd.Text = dt.Rows[0]["password"].ToString();
}
btnupdate.Visible = true;
}
catch
{
throw;
}
}
private void deletedata()
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString);
SqlCommand cmd = new SqlCommand("TF_deletedetails", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", hfname.Value);
con.Open();
int res = cmd.ExecuteNonQuery();
con.Close();
if (res > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('DATA DELETED');", true);
}
}
catch
{
throw;
}
}
add this in the page load at the top
if(ispostback) { return; }
just give a try
when ever u click the grid button as it is a server side control it creates a postback and that is the reason y u face this..............
this link will help u
http://www.c-sharpcorner.com/UploadFile/9f0ae2/gridview-edit-delete-and-update-in-Asp-Net
这篇关于我想编辑并从网格中删除一行,但是我在选择任何一行时默认选择第一行并编辑它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!