在可编辑模式下以行数据显示另一页面或其他网格视图中的gridview数据

时间:2013-06-10 05:11:51

标签: asp.net gridview

当我点击编辑时,我有一个包含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>

1 个答案:

答案 0 :(得分: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,并可以从数据库中获取记录以供进一步处理。

我希望它能帮到你