GridView中无法删除和编辑数据

godtan 2009-01-17 08:04:40
我在学习使用GridView控件任务小窗口中自动生成的 编辑、删除、选择 功能。可是在运行时,点击 删除 却毫无反应;点击 编辑,在更改并点击 更新 之后,又恢复原状。这是为什么呢?

还请各位高手帮忙……不胜感激
...全文
583 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
律己修心 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gxh7506 的回复:]
随便给你搜了个,没调试过,你参考下:
aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
pr…

[/Quote]
Thanx
godtan 2009-01-18
  • 打赏
  • 举报
回复
问题解决了,确实可以不用写代码的。已删除为例,SqlDataSource 控件的 DeleteQuery 属性必须包含一个 SQL Delete 语句,我看了一下,delete语句确实存在,不过其中的变量@original_ID的参数源默认是None。我把它改成了Control就可以了。

在VS2005中,默认只有@original_ID 这一个参数,而在VS2008中,基本每一个字段都以这样一个类似的参数,我就把@original_ID 以外的都给删掉了,然后更改参数源,于是也可以了……

呵呵,谢谢大家了!
yagebu1983 2009-01-17
  • 打赏
  • 举报
回复
去msdn看看!!
godtan 2009-01-17
  • 打赏
  • 举报
回复
但是我在MSDN中查了一下,是这样说的:


使用智能面板启用默认删除功能
右键单击 GridView 控件,再单击“显示智能标记”。

在智能标记面板中选择“启用删除”。

注意:
仅当 GridView 控件所绑定到的数据源控件支持删除功能时,才会在智能标记面板中显示“启用编辑”复选框。[color=#FF9900]例如,如果 GridView 控件绑定到 SqlDataSource 控件,则 SqlDataSource 控件的 DeleteQuery 属性必须包含一个 SQL Delete 语句。
我在我这里看了一下,有那个delete语句,不过其中的@original_ID参数的参数源被设置成None,备选项有Cookie、control、form、profile、querrystring、session等,是不是在这里应该做什么?
此外,还可以设置 GridView 控件属性。

使用 AutoGenerateDeleteButton 属性启用删除功能
选择 GridView 控件,然后在“属性”窗口中将“AutoGenerateDeleteButton”设置为 true。

- 或 -

在“源”视图的 <asp:GridView> 元素中,将 AutoGenerateDeleteButton 属性设置为 true,如下面的示例所示:

复制代码
<asp:GridView Runat="server" ID="GridView1" AutoGenerateDeleteButton="true" />
[/color]
Lovely_baby 2009-01-17
  • 打赏
  • 举报
回复
删除按钮要把那个叫CommandName的属性改为Delete,然后点GridView那个叫SelectedIndexChanging的事件,在里头写代码
gggg747636 2009-01-17
  • 打赏
  • 举报
回复
据算你通过按钮, 除非你不在gridview 窗口中修改。。


在弹出式的对话框修改 , 才不需要些事件。


不然还是要写那些事件啊
godtan 2009-01-17
  • 打赏
  • 举报
回复
好复杂的例子……

我只是想问一下,如果我在GridView的任务小窗口添加删除和编辑两个按钮后,如果要编代码,是不是写一个函数,然后在属性里把事件和函数绑定就行了?
如果是,那么应该写一个什么样的函数?跟哪个事件绑定?

麻烦各位了……
孟子E章 2009-01-17
  • 打赏
  • 举报
回复
.net sdk里面都有详细的例子
ahhfclt 2009-01-17
  • 打赏
  • 举报
回复
哈哈,正好能解决我刚问的那个问题,真巧,问下楼上的,是不是用+变量+传递变量到sql中,
gxh7506 2009-01-17
  • 打赏
  • 举报
回复
随便给你搜了个,没调试过,你参考下:
aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
//创建连接
SqlConnection con = new SqlConnection("server=.;uid=sa;password=sa;database=pubs");
SqlDataAdapter da = new SqlDataAdapter("select * from jobs", con);
//数据集
DataSet ds = new DataSet();
da.Fill(ds, "jobs");
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
/// <summary>
/// 删除事件
/// vs.net 2003的删除事件
/// private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
/// {
/// int id=int.Parse(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
///
/// }
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(this.GridView1.Rows[e.RowIndex].Cells[0].Text);
string sql = "delete from jobs where job_id="+id+"";
Delete(sql);
Bind();
}
/// <summary>
/// 编辑事件
/// vs.net 2003编辑事件
/// private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
/// {
/// this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
/// }
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
Bind();
}
/// <summary>
/// (编辑)更新事件
/// vs.net 2003中(编辑更新事件)
/// private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
/// {
/// string studentPass=((TextBox)e.Item.Cells[2].Controls[0]).Text;
/// int sex = int.Parse(((RadioButtonList)e.Item.Cells[3].FindControl("RadioButtonList1")).SelectedItem.Value);
/// string birthday = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
/// string EMail = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
/// }
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = int.Parse(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
string desc = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string min = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string max = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string sql = "update jobs set job_desc='" + desc + "',min_lvl=" + min + ",max_lvl=" + max + " where job_id="+ id+"";
Update(sql);
this.GridView1.EditIndex = -1;
Bind();
}
public void Update(string sql)
{
SqlConnection con = new SqlConnection("server=.;uid=sa;password=sa;database=pubs");
SqlCommand com = new SqlCommand(sql, con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
public void Delete(string sql)
{
SqlConnection con = new SqlConnection("server=.;uid=sa;password=sa;database=pubs");
SqlCommand com = new SqlCommand(sql, con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
/// <summary>
/// (编辑)取消事件
/// vs.net 2003的(编辑)取消事件
/// private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
/// {
/// DataGrid1.EditItemIndex=-1;
/// }
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
this.Bind();
}
}




aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>

<!--实现对GridView删除行时弹出对话框
将CommandField删除字段转换为TemplateField并且添加属性OnClientClick="return confirm('您确认删除该记录吗?')"
用HyperLinkField向页面传参
DataNavigateUrlFields="参数" DataNavigateUrlFormatString="page.aspx?id={0}"
-->
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Style="position: relative" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<Columns>
<asp:BoundField HeaderText="编号" DataField="job_id" />
<asp:BoundField HeaderText="内容" DataField="job_desc" />
<asp:BoundField HeaderText="最小值" DataField="min_lvl" />
<asp:BoundField HeaderText="最大值" DataField="max_lvl" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" OnClientClick="return confirm('您确认删除该记录吗?')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField HeaderText="查看" Text="查看" DataNavigateUrlFormatString="page.aspx?id={0}" DataNavigateUrlFields="desc" />
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>
godtan 2009-01-17
  • 打赏
  • 举报
回复
无语了,书上说不用写代码的……

那么,应该要写什么代码呢?能不能给个例子?
gxh7506 2009-01-17
  • 打赏
  • 举报
回复
还需要自己写些代码才行,不是拖了控件就可以用的

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧