2012-01-29 116 views
0

我正在建设一个网站,但有一个问题。 我不知道如何访问点击该按钮的网格数据。如何获取网格视图的单击按钮行的数据?

.aspx文件:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlEntry" 
    CssClass="style1"> 
    <Columns> 
     <asp:BoundField DataField="ReordID" HeaderText="ReordID" InsertVisible="False" SortExpression="ReordID" 
      Visible="False" /> 
     <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
     <asp:BoundField DataField="EmailID" HeaderText="EmailID" SortExpression="EmailID" /> 
     <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" /> 
     <asp:TemplateField HeaderText="Delete" SortExpression="Delete"> 
      <ItemTemplate> 
       <asp:Button Text="Delete" runat="server" OnClick="Grid_Click" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
    <HeaderStyle BackColor="Gray" /> 
    <AlternatingRowStyle BackColor="#CCCCCC" /> 
</asp:GridView> 
<asp:SqlDataSource ID="SqlEntry" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Entry]"></asp:SqlDataSource> 

.CS文件:

protected void Refresh_Click(object sender, EventArgs e) 
{ 
    GridView1.DataBind(); 
    resetdata(); 
} 
protected void Submit_Click(object sender, EventArgs e) 
{ 
    string str = "INSERT INTO Entry (Name, EmailID, Password) VALUES ('" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() + "','" + TextBox3.Text.Trim() + "');"; 
    Connection conn = new Connection(str); 
    Refresh_Click(sender, e); 
} 
protected void resetdata() 
{ 
    TextBox1.Text = ""; 
    TextBox2.Text = ""; 
    TextBox3.Text = ""; 
} 
protected void Grid_Click(Object sender, EventArgs e) 
{ 
    string str = "DELETE FROM Entry WHERE RecordID = @RecordID"; 
    Connection conn = new Connection(str); 
    GridView1.DataBind(); 
    resetdata(); 
} 

连接类:

public Connection(string qry) 
{ 
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandType = System.Data.CommandType.Text; 
    cmd.CommandText = qry; 
    cmd.Connection = con; 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
} 

我如何使用此网页的SQL Server 2005中删除的数据? 代码中的问题是什么?

回答

0

你可以附上CommandArgumentCommandName到您的按钮包含ID -

<ItemTemplate> 
    <asp:Button Text="Delete" runat="server" CommandArgument="<%# Eval('ReordID') %>" CommandName="REMOVE" OnClick="Grid_Click" /> 
</ItemTemplate> 

...然后RowCommand事件添加到GridView -

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) 
{ 

    if(e.CommandName=="REMOVE") 
    { 
     int orderId = Convert.ToInt32(e.CommandArgument); 
     //Do Sql Here 
    } 
} 
+0

发生有错误:解析器错误 第50行: Line 51: Line 52:第五十五行:命令名=“REMOVE”/> 第54行:<第三行:<删除行> = <删除行> = <服务器行><删除行> = <<%#Eval(“ReordID”/ItemTemplate> – Ankur 2012-01-30 09:08:13

+0

您正在使用哪个版本的.NET? – Evan 2012-01-30 14:49:03

+0

微软的Visual Studio 2008 .NET框架工作3.5 的Microsoft SQL Server 2005 – Ankur 2012-01-30 17:18:34