Gridview 中所有行都有一个删除按钮
我有一个像这样的简单页面。 (EKLE = ADD
, SıL = DELETE
)
我的 AVUKAT 桌子是这样的。
简化,当我选择第一个下拉列表 MUSTERI 和第二个下拉列表 AVUKAT 时,添加数据库(获取 HESAP (数字)自动)或删除数据库和gridview。
这是我的代码。
添加点击
protected void Add_Click(object sender, EventArgs e)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
/*GridView1.DataSource = dr;
GridView1.Visible = true;*/
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
删除点击
protected void Delete_Click(object sender, EventArgs e)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("DELETE FROM AVUKAT WHERE MUSTERI = @MUSTERI AND AVUKAT = @AVUKAT AND HESAP = @HESAP", myConnection);
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
/* GridView1.DataSource = dr;
GridView1.Visible = true;*/
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
我的经理想要的,删除过程非常困难。让我们轻松一下。因为当我想删除一些数据时,我必须选择两个下拉列表,然后选择删除(Sil)按钮。
我们应该更喜欢每一行都有一个删除按钮。然后当我们点击按钮时。然后必须删除gridviewAND
databse。
我在互联网上找到了关于我想要的完美例子。
我想如果我们能做到这一点,Sil(Delete) 按钮应该被删除。
我们怎样才能做到这一点?
我认为 AutoDeleteButton
不能像这样工作。正确的?它刚刚从 Gridview 中删除。不是数据库。但我想删除两者(Gridview AND
数据库)
I have a simple page like this. (EKLE = ADD
, SİL = DELETE
)
And my AVUKAT Table like this.
Simplify, When i choose first dropdown MUSTERI and second dropdown AVUKAT , add the database (getting HESAP (number) automaticly) or delete the database and gridview.
This is my code.
ADD Click
protected void Add_Click(object sender, EventArgs e)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
/*GridView1.DataSource = dr;
GridView1.Visible = true;*/
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
DELETE Click
protected void Delete_Click(object sender, EventArgs e)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("DELETE FROM AVUKAT WHERE MUSTERI = @MUSTERI AND AVUKAT = @AVUKAT AND HESAP = @HESAP", myConnection);
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
/* GridView1.DataSource = dr;
GridView1.Visible = true;*/
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
My manager wants, delete process is so hard. Let's make easy. Because when i want to delete some data, i must choose two dropdownlist and then delete(Sil) button.
We should prefer that every row has a delete button. Then when we click the button. Then must be deleted gridview AND
databse.
I found perfect example on the internet abuot what i want.
I think if we can do that, Sil(Delete) Button should be deleted.
How can we do that?
I think AutoDeleteButton
don't work like this. Right? It just deleted from Gridview. Not Database. But i want deleting both (Gridview AND
Database)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要确保调用正确的方法。
http://msdn.microsoft.com/en -us/library/system.web.ui.webcontrols.gridview_events.aspx
单击该按钮将运行“RowDeleting”方法,然后运行“RowDeleted”。
编辑 - 忘记提及,一旦运行已删除的方法,您将需要重新绑定网格视图。
GridView1.DataBind()
You need to ensure you calling the correct method.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events.aspx
Clicking the button will run the "RowDeleting" method then "RowDeleted".
Edit - forgot to mention, once you've run your deleted method you'll need to rebind the gridview.
GridView1.DataBind()