【发布时间】:2017-02-02 10:40:41
【问题描述】:
我是 c# 和 asp.net 的新手,我正在尝试制作一个管理项目集合的网络表单。
我已经设法为添加/搜索/编辑制作表格,但我在删除时遇到了一些问题。
我的错误是:
-错误 CS0103 当前上下文中不存在名称“chk”(try.aspx.cs 行:74)
-错误 CS0119 'CheckBox' 是一种类型,在给定的上下文中无效(try.aspx.cs 行:74)
我看到几个问题都出现相同的错误,但我仍然无法弄清楚我的错误在哪里。
try.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="try.aspx.cs" Inherits="keszlet_management._try" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="KeszletGrid" runat="server" AllowPaging="True" AllowSorting="True" OnSelectedIndexChanged="KeszletGrid_SelectedIndexChanged" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="Item"
HeaderText="Item" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField ItemStyle-Width="150px" DataField="Type"
HeaderText="Type" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField ItemStyle-Width="150px" DataField="Owner"
HeaderText="Owner" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField ItemStyle-Width="150px" DataField="ID" Visible="false" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:Button ID="DelBtn" runat="server" Text="Delete Selected Rows" OnClick="DelBtn_Click" />
<asp:Label ID="Label1" runat="server" Text="Msg"></asp:Label>
</asp:Content>
try.aspx.cs:
using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace keszlet_management
{
public partial class _try : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet();
}
private void DataSet()
{
if (!IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM items"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
KeszletGrid.DataSource = dt;
KeszletGrid.DataBind();
}
}
}
}
}
}
protected void DelBtn_Click(object sender, EventArgs e)
{
int Count = 0;
KeszletGrid.AllowPaging = false;
KeszletGrid.DataBind();
ArrayList arr = (ArrayList)ViewState["SelectedRecords"];
Count = arr.Count;
for(int i=0;i<KeszletGrid.Rows.Count;i++)
{
if(arr.Contains(KeszletGrid.DataKeys[i].Value))
{
DeleteRecord(KeszletGrid.DataKeys[i].Value.ToString());
arr.Remove(KeszletGrid.DataKeys[i].Value);
}
}
ViewState["SelectedRecords"] = arr;
KeszletGrid.AllowPaging = true;
DataSet();
}
private void GetData()
{
int i;
ArrayList arr;
if (ViewState["SelectedRecords"] != null)
arr = (ArrayList)ViewState["SelectedRecords"];
else
arr = new ArrayList();
for (i = 0; i < KeszletGrid.Rows.Count; i++)
if (CheckBox chk = (CheckBox)KeszletGrid.Rows[i].Cells[0].FindControl("chk"))
{
if (chk.Checked)
{
if (!arr.Contains(KeszletGrid.DataKeys[i].Value))
{
arr.Add(KeszletGrid.DataKeys[i].Value);
}
}
else
if (arr.Contains(KeszletGrid.DataKeys[i].Value))
{
arr.Remove(KeszletGrid.DataKeys[i].Value);
}
}
ViewState["SelectedRecords"] = arr;
}
private void DeleteRecord(string ID)
{
string contrs = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
string query = "Delete From items Where id=@ID";
MySqlConnection con = new MySqlConnection(contrs);
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("@ID", ID);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
【问题讨论】:
-
尝试改用
(CheckBox)KeszletGrid.Rows[i].FindControl("chk")。 -
这个link 可以帮助你。