2017-02-02 30 views
0

私は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(); 
     } 
    } 
} 
+0

http://stackoverflow.com/questions/18233630/variable-does-not-exist-in-the-current-context – ColinM

+1

使用してみてください '(チェックボックス)KeszletGrid .Rows [i] .FindControl( "chk") '代わりに。 – user1429080

+0

この[リンク](http://stackoverflow.com/questions/19628554/gridview-get-checkbox-checked-value)が役立ちます。 –

答えて

0

私は、これはあなたを助けることを願っています:

for (i = 0; i < KeszletGrid.Rows.Count; i++) 
    if (CheckBox chk = KeszletGrid.Rows[i].Cells[0].Controls[0] as CheckBox; 

またはこれ試してください:あなたが使用していない以下の

にごGetData方法を更新し

foreach(GridViewRow row in KeszletGrid.Rows) { 
    if(row.RowType == DataControlRowType.DataRow) { 
    CheckBox chk = row.FindControl("chk ") as CheckBox ; 
    } 
} 
+0

メインエラーはまだありますが、CheckBoxまたはchk(CheckBox chk = ...)に変更が加えられていません –

+0

私の編集が参考になる場合があります – kritikaTalwar

+0

@kritikaTalwar、下記のAlanのコメントを参照してください。 if条件内では、既存の変数のみを割り当てることができます。 – ColinM

0

このライン74か?

if (CheckBox chk = (CheckBox)KeszletGrid.Rows[i].Cells[0].FindControl("chk")) 

chkを宣言して、同じ行の条件で使用することはできません。これを試してみてください:

CheckBox chk = KeszletGrid.Rows[i].Cells[0].FindControl("chk") as CheckBox 
if (chk != null && chk.Checked) { 
    // etc. 
} 
+0

チェックボックスは 'TemplateField'にありますので、行の' Cell'にはありません。行の 'FindControl'を使う必要があります。しかし、もしかすると奇妙な使い方でうまくキャッチする。 – user1429080

+0

@ user1429080元のコードは行内のセルで探していますが、そうではありませんか?私はあなたの行をコピーし、型宣言を削除しました。 –

+0

私はOPではない:-)。あなたの提案は、私が信じている通り、正しい道のりにありますが、ゴールまでは至っていません。私がコメントした理由です... – user1429080

0

forループ内のコードブロック、およびいくつかのコードブロックがありません。方法を粗くする。コードの1行、複数でない場合がありますif文でインデント

のみ動作します。不足している1または2つのに近いブレースもありました

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++) 
    { 
     CheckBox chk = null; 
     if ((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); 
       } 
      } 
     } 
    } 
} 
関連する問題