目前想做一个类似 用datagrid的例子,于是照着一个datagrid的代码改。
在.aspx中添加了 <asp:ButtonField Text="delete" HeaderText="delete" CommandName="Delete" ButtonType="Button"> </asp:ButtonField> 在.aspx.cs中添加了
protected void Gridview_Delete(object sender, GridViewCommandEventArgs e)
{//use WEBCONFIG connection
string conn = ConfigurationSettings.AppSettings["strconn"];
SqlConnection st = new SqlConnection(conn);
String delsql = "delete from stock where stid=@stid";
SqlCommand mycommand = new SqlCommand(delsql,st);
mycommand.Parameters.Add(new SqlParameter("@stid",SqlDbType.Int));
mycommand.Parameters["@stid"].Value=GridView1.DataKeys[(int)e.Item.ItemIndex];
st.Open();
try{
mycommand.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('delete succeed')</script>"); }
catch(SqlException)
{
Response.Write("<script language=javascript>alert('delete failed')</script>"); }
st.Close();
BindGrid();
}可是调试报:'System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'Item'
出错语句是:mycommand.Parameters["@stid"].Value=GridView1.DataKeys[(int)e.Item.ItemIndex];看书上的例子也没有找到定义Item的地方
请问,在使用gridview中,这个Item是如何定义的呢?
或者有其他解决办法来解决在gridview中添加一个按钮实现删除的功能呢。。
在.aspx中添加了 <asp:ButtonField Text="delete" HeaderText="delete" CommandName="Delete" ButtonType="Button"> </asp:ButtonField> 在.aspx.cs中添加了
protected void Gridview_Delete(object sender, GridViewCommandEventArgs e)
{//use WEBCONFIG connection
string conn = ConfigurationSettings.AppSettings["strconn"];
SqlConnection st = new SqlConnection(conn);
String delsql = "delete from stock where stid=@stid";
SqlCommand mycommand = new SqlCommand(delsql,st);
mycommand.Parameters.Add(new SqlParameter("@stid",SqlDbType.Int));
mycommand.Parameters["@stid"].Value=GridView1.DataKeys[(int)e.Item.ItemIndex];
st.Open();
try{
mycommand.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('delete succeed')</script>"); }
catch(SqlException)
{
Response.Write("<script language=javascript>alert('delete failed')</script>"); }
st.Close();
BindGrid();
}可是调试报:'System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'Item'
出错语句是:mycommand.Parameters["@stid"].Value=GridView1.DataKeys[(int)e.Item.ItemIndex];看书上的例子也没有找到定义Item的地方
请问,在使用gridview中,这个Item是如何定义的呢?
或者有其他解决办法来解决在gridview中添加一个按钮实现删除的功能呢。。
解决方案 »
- |zyciis| 求得出数据库中的未有的最大编号,谢谢
- 问一个css问题啊,会的帮忙下!
- 请教asp.net中页面历史记录的问题?
- forms验证问题
- 为什么,调试时,我的页面上看不到控件。(C#2003,asp.net)来者有分
- 跪求:Wap在uniwap接入点下下载非80端口服务器上的文件不成功!
- 如何在excel里把“\r\n”显示为换行?
- 子网掩码改大了点,为什么数据库就连接不上
- 如何在DataList中判断时间,且在天与天之间的数据中输出日期?(ASP.NET--C#)
- 关于站内搜索的问题及判断用户是否登陆的问题~`~~高手来指教!!!
- 应用Asp.net ajax的问题
- 正则表达式怎么取<!--start--><!--end-->中间的内容
Dim conn As New OleDbConnection
conn.ConnectionString = StrCon
conn.Open()
Dim sid As Int16 = myDataGrid.DataKeys.Item(e.Item.ItemIndex)
strsql = "delete from akind where id=" & sid & ""
Dim strcomm As New OleDbCommand(strsql, conn)
strcomm.ExecuteNonQuery()
conn.Close()
Label3.Text = "成功删除分类"
Label3.ForeColor = Drawing.Color.Red
strsql = "SELECT id,kind1,kind2,mcon from akind where uname='" & Session("acountuser") & "'"
BindList()
End Sub
Sub button2_click(ByVal s As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rowindex As Int16
Dim suc As Boolean = False
GridView1.EditIndex = -1
Dim conn As New OleDbConnection
conn.ConnectionString = StrCon
conn.Open()
Dim strcomm As New OleDbCommand
strcomm.Connection = conn
For rowindex = 0 To GridView1.Rows.Count - 1
If CType(GridView1.Rows(rowindex).FindControl("chkSelect"), CheckBox).Checked = True Then
strsql = "delete from shouzhi where id=" & GridView1.DataKeys.Item(rowindex).Value & ""
strcomm.CommandText = strsql
strcomm.ExecuteNonQuery()
suc = True
End If
Next
conn.Close()
strsql = ViewState("strsql")
BindList()
If suc = True Then
Label3.Text = "删除成功"
Label3.ForeColor = Drawing.Color.Red
Else
Label3.Text = "未选择任何项目"
Label3.ForeColor = Drawing.Color.Red
End If
End Sub
现在是row没有定义。。另外有点基础疑问请教,在.aspx中的CommandName="Delete" 是如何与
.aspx.cs中的protected void Gridview_Delete 关联起来呢,程序怎么知道我点击button是执行了 Gridview_Delete 这个函数呢?