2017-02-23 4 views
1

今日、私はgridviewに関するすべてを学んでいます。私のGridViewの内容を要約すると、ここにある:カスタム削除を作成し、asp.netを使用してgridviewで編集しますか?

Id                       Type      Name     Image
Delete Edit 1     Guitar    Ibanez    pic1.jpg
Delete Edit 2     Guitar    Fender    pic2.jpg

[LabelId]
[LabelName]

私は何をしようとしているで、いつでも私は、ボタンを削除または編集、更新ボタンは、それが列名の値を取得する必要がありをクリックし。たとえば、2行目の編集ボタンをクリックすると、「Fender」という名前が取得され、[LabelName]とそれに加えて[LabelId]に表示されます。また、1行目の削除ボタンをクリックした場合、「Ibanez」という名前が取得され、下のラベルにも表示されます。同じことが更新ボタンにも当てはまります。それは常に名前を表示する必要があります、私は編集、削除、および更新ボタンがクリックされるたびにします。

私はこれのためのコードを作成しようとしましたが、名前だけではなく、常に空白です。ここで

aspxコードです:

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

<!DOCTYPE html> 

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

</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1"> 
     <Columns> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Button3_Click"/> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField ShowHeader="False"> 
       <EditItemTemplate> 
        <asp:Button ID="ButtonUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update" OnClick="ButtonUpdate_Click"/> 
        &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Button ID="ButtonEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" OnClick="ButtonEdit_Click"/> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="id" SortExpression="id"> 
       <EditItemTemplate> 
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="type" SortExpression="type"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("type") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label2" runat="server" Text='<%# Bind("type") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="name" SortExpression="name"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label3" runat="server" Text='<%# Bind("name") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="image" SortExpression="image"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("image") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label4" runat="server" Text='<%# Bind("image") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 

     </Columns> 
    </asp:GridView> 

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BrandsDBConnectionString %>" DeleteCommand="DELETE FROM [guitarBrands] WHERE [id] = @id" InsertCommand="INSERT INTO [guitarBrands] ([id], [type], [name], [image]) VALUES (@id, @type, @name, @image)" SelectCommand="SELECT [id], [type], [name], [image] FROM [guitarBrands]" UpdateCommand="UPDATE [guitarBrands] SET [type] = @type, [name] = @name, [image] = @image WHERE [id] = @id"> 
     <DeleteParameters> 
      <asp:Parameter Name="id" Type="Int32" /> 
     </DeleteParameters> 
     <InsertParameters> 
      <asp:Parameter Name="id" Type="Int32" /> 
      <asp:Parameter Name="type" Type="String" /> 
      <asp:Parameter Name="name" Type="String" /> 
      <asp:Parameter Name="image" Type="String" /> 
     </InsertParameters> 
     <UpdateParameters> 
      <asp:Parameter Name="type" Type="String" /> 
      <asp:Parameter Name="name" Type="String" /> 
      <asp:Parameter Name="image" Type="String" /> 
      <asp:Parameter Name="id" Type="Int32" /> 
     </UpdateParameters> 
    </asp:SqlDataSource> 

    <br/> 
    <asp:Label ID="lblId" runat="server" Text="Label"></asp:Label><br/> 
    <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label><br/> 
</form> 

そしてここでaspx.csコードである:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

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

SqlConnection con1; 
SqlCommand cmd1; 
DataSet ds1; 
public _Default() 
{ 
    con1 = new SqlConnection(); 
    con1.ConnectionString = ConfigurationManager.ConnectionStrings["BrandsDBConnectionString"].ToString(); 
    cmd1 = new SqlCommand(); 
    ds1 = new DataSet(); 
} 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     bindgridviewguitarbrands(); 
    } 
} 

//Start of Gridview Code for Guitar Brands 
private void bindgridviewguitarbrands() 
{ 

    con1.Open(); 
    cmd1.CommandText = "SELECT * FROM [guitarBrands]"; 
    cmd1.Connection = con1; 
    SqlDataAdapter da1 = new SqlDataAdapter(cmd1); 
    da1.Fill(ds1); 
    con1.Close(); 
    GridView1.DataBind(); 

} 

protected void Button3_Click(object sender, EventArgs e) 
{ 

     Button btn1 = sender as Button; 
     GridViewRow gridrow = btn1.NamingContainer as GridViewRow; 
     int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString()); 
     string name = GridView1.Rows[gridrow.RowIndex].Cells[4].Text; 

     lblId.Text = id.ToString(); 
     lblName.Text = name; 

} 

protected void ButtonEdit_Click(object sender, EventArgs e) 
{ 

    Button btn2 = sender as Button; 
    GridViewRow gridrow = btn2.NamingContainer as GridViewRow; 
    int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString()); 
    string name = GridView1.Rows[gridrow.RowIndex].Cells[4].Text; 
    lblId.Text = id.ToString(); 
    lblName.Text = name; 

} 

protected void ButtonUpdate_Click(object sender, EventArgs e) 
{ 
    Button btn3 = sender as Button; 
    GridViewRow gridrow = btn3.NamingContainer as GridViewRow; 
    int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString()); 
    string name = GridView1.Rows[gridrow.RowIndex].Cells[4].Text; 

    lblId.Text = id.ToString(); 
    lblName.Text = name; 

} 

} 

の大部分になります。このコードのための答えを持って私のプロジェクトを終わらせる。うまくいけば、あなたは私にこのことを助けることができます。

答えて

2

セルにはTextは含まれていませんが、Labelを含んでいます。私はコードを試してみましたが、それは私にこのエラーを与えている

var cell = GridView1.Rows[gridrow.RowIndex].Cells[4]; 
string name = ((Label)cell.FindControl("Label1")).Text; 
+0

:私はこれをしようとするだろう**「テーブルセルは」「FindControls」とタイプの最初の引数を受け入れていない拡張メソッド「FindControls」の定義が含まれていません。 'TableCell'が見つかりました(使用するディレクティブまたはアセンブリ参照がありませんか?)。** – BrunoEarth

+0

申し訳ありませんが、それは 'FindControl'です。最後に' s'はありません。私はこれを記憶からやろうとしていました。 –

+0

はい、私は数時間前にそれを変更することができましたが、今度はエラーが.Textに行きました。なぜ私はそれに切り替わったのかわかりません。テキスト、それは同じエラーがあります。 – BrunoEarth

関連する問題