【问题标题】:how to update only child GridView?如何仅更新子 GridView?
【发布时间】:2012-03-31 06:20:54
【问题描述】:

这是我现在正在做的事情:

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridViewUserScraps_RowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                 <asp:TextBox ID="MakeComments" runat="server" TextMode="MultiLine"></asp:TextBox>
                 <asp:Button ID="btnPost" Text="Comment" runat="server" CommandName="Comment"  CommandArgument='<%#Eval("ScrapId")%>' />
                <asp:GridView ID="GridView2" runat="server">
                   <%--this GridView2 showing comments--%>
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

protected void GridViewUserScraps_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridView gv = new GridView();
    gv = (GridView)row.FindControl("GridView2");
    //getting data to bind child gridview.
    gv.DataSource = td;
    gv.DataBind();
}

所以,在单击 GridView1 按钮时,我正在更新数据库并同时获取数据,这没有任何问题。但为此我必须绑定/刷新父(GridView1)Gridview,因为有近 50 行,所以过程相当缓慢。我正在寻找的是; 我只想更新或刷新 GridView2 以显示添加的 cmets。

【问题讨论】:

  • 您想何时刷新 cmets?在单击按钮期间?
  • @PraVn OP 想要更新用户点击按钮所在行的 GridView2。
  • 因为我在 GridView1 的 OnRowDataBound 事件中绑定了 GridView2,所以我必须更新 GridVeiw1,此后只有我得到更新的数据。是的,我想在 Button Click 上刷新它。

标签: c# asp.net gridview


【解决方案1】:

GridView1.RowCommand 是处理按钮操作的正确事件。

GridView1.RowCommand += (o, e) =>
    {
        var row = (e.CommandSource as Button).NamingContainer as GridViewRow;
        var makeComments = row.FindControl("MakeComments") as TextBox;
        int scrapId = Int32.TryParse((string)e.CommandArgument, out scrapId) ? scrapId : 0;
        var gridView2 = row.FindControl("GridView2") as GridView;

        ... place here the code which the comments

        gridView2.DataSource = GetCommentsByScrapId();
        gridView2.DataBind();
    };

在此事件(或其他事件)上,父级不会绑定,除非您指定它。 绑定控件的一个常见原因是每次页面加载时错误地调用DataBind()。为了防止这种情况,需要在页面的第一个请求上进行绑定,正确的方法是检查 IsPostBack 是否为 false。

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                 GridView1.DataSource = GetUserScraps();
                 GridView1.DataBind();
            }
        }

【讨论】:

  • 感谢阿德里安的回复。让我检查一下您的样品,我会尽快回复。
  • 完全忽略了这个答案,我的几乎一模一样。哎呀
  • 是的!这行得通。 var row1 = (e.CommandSource as Button).NamingContainer as GridViewRow; var gridView2 = row1.FindControl("GridView2") as GridView;
【解决方案2】:

您可以只刷新子网格视图而不刷新父网格视图。 为此,您可以对按钮进行客户端单击 (ClientClick) 并调用 jquery 函数,而不是服务器端单击。 然后,您可以通过 ajax 调用从特定 jquery 函数调用 web 方法或 http 处理程序类,仅更新子 gridview 中的项目。 在这里,您的父网格不会刷新,因为单击按钮时没有服务器命中。 希望这会有所帮助,如果您需要,我将提供 jquery 函数的示例代码。

【讨论】:

  • 我认为上面的示例就足够了。如果您想现场查看,请访问我的网站:mitracircle.com
  • @Sugandika:感谢您的回复。但是,我在 GridView1 的 OnRowDataBound 事件中绑定了 GridView2,所以我必须更新 GridVeiw1,此后只有我得到更新的数据。我需要任何替代解决方案。
【解决方案3】:

试试这个

foreach(GridViewRow rIndex in GridView1.Rows)
{

GridView gv = new GridView();
    gv = (GridView)row.FindControl("GridView2");
    //getting data to bind child gridview.
   // if you want to get the row key value use GridView1.DataKeys[rIndex.RowIndex].Value
    gv.DataSource = td;
    gv.DataBind();

}

【讨论】:

  • 您是否要求将此代码放入 GridViewUserScraps_RowDataBound 事件中。如果是这样,我们仍然会存在问题,如果不向我展示样品。我确实需要获取 GridView1 的 RowIndex 值来获取基于此的评论数据。
  • 可以把上面的代码放在按钮点击中。然后要获取父 id,您可以使用 GridView1.DataKeys[rIndex.RowIndex].Value 可用于检索 cmets。
【解决方案4】:

我会设置按钮的命令名,然后使用 gridview 的 OnRowCommand 事件

<asp:gridview id="GridView1" runat="server" onrowdatabound="GridViewUserScraps_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:TextBox ID="MakeComments" runat="server" TextMode="MultiLine"></asp:TextBox>
            <asp:Button ID="btnPost" Text="Comment" runat="server" CommandName="UpdateChildGrid" />
            <asp:GridView ID="GridView2" runat="server">
                <%--this GridView2 showing comments--%>
            </asp:GridView>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

代码背后:

protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    if(e.CommandName=="UpdateChildGrid")
    {
        GridViewRow row = (e.CommandSource as Button).NamingContainer as GridViewRow;
        GridView child = row.FindControl("GridView2") as GridView;
        // update child grid
    }
  }

根据记忆进行此操作,因此可能不准确,但应该足够接近,让您知道要去哪里

【讨论】:

  • 更新了它,当它应该是(e.CommandSource as Button)时将它设置为(sender as Button)
【解决方案5】:

GridView HTML

<asp:gridview id="GridView1" runat="server" onrowdatabound="GridViewUserScraps_RowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="MakeComments" runat="server" TextMode="MultiLine"></asp:TextBox>
                <asp:Button ID="btnPost" Text="Comment" runat="server" OnClick="btnPost_Click" />
                <asp:GridView ID="GridView2" runat="server">
                    <%--this GridView2 showing comments--%>
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:gridview>

代码隐藏

protected void btnPost(object sender, EventArgs e)
{
    //Your code for other operations
    //
    GridView GridView2 = (GridView)((GridViewRow)((Button)sender).NamingContainer).FindControl("GridView2");
    GridView2.DataSource = YourDataBasefetchingFunction();
    GridView2.DataBind() 
}

注意 - 将ScrapID DataItem 转换为DataKey

【讨论】:

    【解决方案6】:

    仅更新/刷新单击更新按钮所在行中的子 GridView2

    首先你需要将整个 GridView1 放在更新面板中。 以及另一个 UpdatePanel 中的子 GridView 然后在按钮上单击 U just hav 以保存您的数据并刷新子网格,这样只会刷新子网格。

    这是一个工作示例。

    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1
    {
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                SqlConnection conn = new SqlConnection("Password=password;Persist Security Info=True;User ID=uid;Initial Catalog=Northwind;Data Source=servername");
                SqlCommand cmd = new SqlCommand("select top 10 * from orders", conn);
                cmd.Connection.Open();
                var dt = cmd.ExecuteReader();
                GridView1.DataSource = dt;
                GridView1.DataBind();
                cmd.Connection.Close();
                cmd.Dispose();
            }
    
        }
    
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
    
        }
    
        protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateChildGrid")
            {
    
                GridViewRow row = (e.CommandSource as Button).NamingContainer as GridViewRow;
                GridView child = row.FindControl("GridView2") as GridView;
    
                string id = row.Cells[0].Text;
    
                SqlConnection conn = new SqlConnection("Password=password;Persist Security Info=True;User ID=uid;Initial Catalog=Northwind;Data Source=servername");
                SqlCommand cmd = new SqlCommand("select top 5 * from [order Details] where OrderID = " + id, conn);
                cmd.Connection.Open();
                var dt = cmd.ExecuteReader();
                child.DataSource = dt;
                child.DataBind();
                cmd.Connection.Close();
                cmd.Dispose();
            }
        }
    
    
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string id = e.Row.Cells[0].Text;
                GridView gv = new GridView();
                gv = (GridView)e.Row.FindControl("GridView2");
                SqlConnection conn = new SqlConnection("Password=password;Persist Security Info=True;User ID=uid;Initial Catalog=Northwind;Data Source=servername");
                SqlCommand cmd = new SqlCommand("select top 5 * from [order Details] where OrderID = " + id, conn);
                cmd.Connection.Open();
                var dt = cmd.ExecuteReader();
                gv.DataSource = dt;
                gv.DataBind();
                cmd.Connection.Close();
                cmd.Dispose();
            }
    
    
        } 
    
    }
    

    }

    还有后面的代码

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    
    
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
        <p>
    
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" 
        AutoGenerateColumns="False" 
        onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="OrderID"  />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                    <ContentTemplate>
                        <asp:TextBox ID="MakeComments" runat="server" TextMode="MultiLine"></asp:TextBox> 
                        <asp:Button ID="btnPost" ButtonType="Button" Text="Comment" runat="server" CommandName="UpdateChildGrid" />
                        <asp:GridView ID="GridView2" runat="server"> 
                        </asp:GridView> 
                    </ContentTemplate>
                    </asp:UpdatePanel>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    
    </asp:GridView>
    </ContentTemplate>
    
    </asp:UpdatePanel>
    </asp:Content>
    

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多