【问题标题】:Click "Accept" button in Gridview then move the record in another Gridview in different page单击 Gridview 中的“接受”按钮,然后在不同页面的另一个 Gridview 中移动记录
【发布时间】:2013-01-06 08:20:48
【问题描述】:

我想要一个具有 "ACCEPT" 按钮的网格视图,然后单击它。我希望在另一个页面中的另一个网格视图中移动或转移接受的记录?

我已经有两个网格视图:

Gridview1 名为 PendingRecordsGridviewGridview2 命名为 AcceptedRecordsGridview

我已经做了一个 register.aspx 页面,点击注册按钮后,它将在 PendingRecordsGridview 中发送数据。它有效!而现在,就像我说的。我想在那个gridview中添加“接受”按钮,所以在点击接受按钮之后。请把记录转移到AcceptedRecordsGridview(另一页)!救命!

【问题讨论】:

  • 你的意思是接受的记录应该出现在已经打开的页面的网格中?
  • 说清楚...PendingRecordsGridview 位于 (pending.aspx) 页面.. 然后 AcceptedRecordsGridvew 位于 (accepted.aspx) 页面。确切地说是两页:)

标签: asp.net gridview expression move transfer


【解决方案1】:

参考这个..我已经测试过了..

你必须记住的事情..

  1. 你必须为你的两个gridview创建两个不同的表。但是那些表设计应该与tbl1和tbl2相同。在tbl1中将一个字段作为主键并使其自动递增

  2. 不要在 tbl2 中保留任何主键或自动递增字段

在接受按钮上,第一个数据将被插入到 tbl2,然后从原始表 tbl1 中删除 page.aspx 文件

            <asp:GridView ID="PendingRecordsGridview" runat="server" 
        AutoGenerateColumns="False" DataKeyNames="id"
        onrowcommand="PendingRecordsGridview_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="Accept">
                <ItemTemplate>
                    <asp:Button CommandArgument='<%# Bind("id") %>' ID="Button1" runat="server" CausesValidation="false" 
                        CommandName="accept" Text="Accept" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="name" SortExpression="name">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="id" SortExpression="id">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="sd1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT * FROM [tbl1]"></asp:SqlDataSource>

现在 page.aspx.cs 文件

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bind();
    }
}
protected void bind()
{
    PendingRecordsGridview.DataSource = sd1;
    PendingRecordsGridview.DataBind();
}
protected void PendingRecordsGridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "accept")
    {
        Session["id"] = e.CommandArgument.ToString();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        con.Open();
            SqlCommand cmd1 = new SqlCommand("INSERT INTO tbl2 (id, name) SELECT id, name FROM tbl1 where id='"+Session["id"].ToString()+"'", con);
            SqlCommand cmd2 = new SqlCommand("delete from tbl1 where id='"+Session["id"].ToString()+"'", con);
            cmd1.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();
            bind();

    }
}

现在只需获取另一个页面..并将其与 tbl2 绑定,该页面将包含已批准的记录...

请随意询问。如果您觉得有帮助,请将其标记为答案

【讨论】:

  • 我忘了 :)) 我使用的是 AccessDataSource 而不是 SQL :)) 但我明白了 :)
  • 能否请您在 Access 中转换您的数据库?请呵呵 :)) 并再次编写代码?请。你会拯救我的一天!超级
  • 对不起,我从来没有使用过 access..bt 是的,您也可以轻松地使用 access..只需使用命令参数和命令字段,您可以通过此 id 引发任何事件,您可以用相同的方式更新数据..用它绑定你的主键字段..我已经使用命令字段作为 id 你可以使用你自己的:))
  • 您的网格视图可能没有在其标记中连接事件。尝试在您的 grideview 代码中添加 onrowcommand="PendingRecordsGridview_RowCommand"
  • 我相信这个问题的原因是由于复制现有页面并将其重命名为其他内容,但没有检查 .aspx.cs 文件中的类是否与 .aspx 文件上的继承相匹配。做一件事,换一个新的页面,然后尝试一步一步地做一件事。否则这将花费太多时间来发现一个小错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多