1

編集をクリックすると、10列のグリッドビューがあり、編集可能モードの行データとテーブルの他の列を含む別のページにデータを表示する必要があります。これから私を助けてください...お願い...、

<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
public partial class displaypage : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(@"Data Source=SRAVI-PC\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        getdata();
    }
    public void getdata()
    {
        string cmd = "select * from namestb";
        SqlDataAdapter da = new SqlDataAdapter(cmd, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("editpage.aspx");
    }
}
} <code>

.aspx

<pre>
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false"         CodeBehind="displaypage.aspx.cs" Inherits="WebApplication1.displaypage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton1" runat="server" Height="30px" 
                        ImageUrl="~/images/edit image.jpg" Width="38px" 
                        onclick="ImageButton1_Click" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <br />
    <br />
</div>
</form>
</body>
</html>
<code>
4

1 に答える 1

7

GridView に RowCommand イベントを追加する必要があります。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
      <asp:LinkButton ID ="lnkEdit" runat ="server" CommandArgument='<%#Eval("Recordid")%>' CommandName ="cmdEdit" Text ='Edit'></asp:LinkButton>
</asp:GridView>

ここで、rocordid はレコードの ID です。

Page behind では、コードを記述する必要があります。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{


    if (e.CommandName == "cmdEdit")
    {
        string Recordid = Convert.ToString(e.CommandArgument.ToString());
        Response.Redirect("EditPage.aspx?recordid="+Recordid );
    }
}

EditPage では、クエリ文字列から recordid を取得し、さらに処理するためにデータベースからレコードを取得できます。

お役に立てば幸いです

于 2013-06-10T05:35:57.433 に答える