私はc#とasp.netを初めて使用しており、アイテムのコレクションを管理するWebフォームを作成しようとしています。
私はすでに追加/検索/編集用のフォームを作成していましたが、削除にはいくつか問題があります。
私のエラーは、次のとおりです。
- エラーCS0103現在のコンテキスト内に存在しない名前「CHK」(try.aspx.csラインを:74)
- エラーCS0119「のチェックボックス」には有効でないタイプがあり、指定されたコンテキスト(try.aspx.csライン:74)名前 'chk'は現在のコンテキストに存在しません
私は同じエラーを持ついくつかの質問を見てきましたが、私の間違いがある場合、私はまだ把握できませんでした。
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 [email protected]";
MySqlConnection con = new MySqlConnection(contrs);
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("@ID", ID);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
http://stackoverflow.com/questions/18233630/variable-does-not-exist-in-the-current-context – ColinM
使用してみてください '(チェックボックス)KeszletGrid .Rows [i] .FindControl( "chk") '代わりに。 – user1429080
この[リンク](http://stackoverflow.com/questions/19628554/gridview-get-checkbox-checked-value)が役立ちます。 –