【问题标题】:display gridview data in another page or other gridview with row data in editable mode以可编辑模式在另一个页面或其他带有行数据的 gridview 中显示 gridview 数据
【发布时间】:2013-06-10 05:11:50
【问题描述】:

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

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    您需要在 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后面需要写代码。

    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,并可以从数据库中获取记录以进行进一步处理。

    希望对你有帮助

    【讨论】:

    • 编译错误说明:在编译服务此请求所需的资源时出错。请查看以下特定错误详细信息并适当修改您的源代码。编译器错误消息:CS1061:“ASP.displaywebform_aspx”不包含“GridView1_RowEditing”的定义,并且找不到接受“ASP.displaywebform_aspx”类型的第一个参数的扩展方法“GridView1_RowEditing”(您是否缺少 using 指令或程序集参考?)
    • 先生,我收到此错误::: 编译错误描述:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。编译器错误消息:CS1061:“ASP.displaywebform_aspx”不包含“GridView1_RowEditing”的定义,并且找不到接受“ASP.displaywebform_aspx”类型的第一个参数的扩展方法“GridView1_RowEditing”(您是否缺少 using 指令或程序集参考?)
    • 你能提供你的代码吗?没有代码我只能猜测。请检查命令的名称(CommandName =“cmdEdit”)。我认为您将命令命名为“编辑”。如果是这样,那么您需要更改命令名称或添加事件“GridView1_RowEditing”。
    • ​​ton>
    • protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { string cmdEdit = "update Customer set FirstName=null, LastName=null, Designation=null, Address=null, City=null, State=null, Country=null 其中 CustomerId="; if (e.CommandName == "cmdEdit") { string Recordid = Convert.ToString(e.CommandArgument.ToString()); Response.Redirect("EditPage.aspx?recordid=" + Recordid); }
    猜你喜欢
    • 2016-01-30
    • 2021-07-23
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多