【问题标题】:All GridView rows disappear while selecting a row选择行时所有 GridView 行都会消失
【发布时间】:2011-06-30 20:23:51
【问题描述】:

请考虑我在调试模式下获得的 cmets 中的值:

protected void FilesGrid_SelectedIndexChanged(object sender, EventArgs e)
{
    int selected = FilesGrid.SelectedIndex; // selected = 2
    FilesGrid.DataBind();  //added after feedback in comments. it makes no change
    int count = FilesGrid.Rows.Count; // count = 0
    GridViewRow row = FilesGrid.Rows[selected]; // row = null
    GridViewRow row0 = FilesGrid.Rows[0]; // row = null
}

我在调查为什么 SelectedValue 在此事件处理程序中给出 null 时遇到此代码(肯定设置了 DataKeyNames 参数)。

谁能解释一下这是怎么可能的?

提前谢谢你。

PS。 这是我的aspx代码:

<asp:GridView ID="FilesGrid" runat="server" AutoGenerateColumns="False" 
    AutoGenerateSelectButton="True"  
    onselectedindexchanged="FilesGrid_SelectedIndexChanged" 
    style="margin-top: 0px" >
    <Columns>
        <asp:CommandField ShowDeleteButton="True" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Length" DataFormatString="{0:N0}" 
            HeaderText="Size in Bytes" HtmlEncode="False" />
    </Columns>
</asp:GridView>

我是这样绑定数据的:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string [] dd = {"FullName"};
        FilesGrid.DataKeyNames = dd;

        string appPath = Request.PhysicalApplicationPath; 
        DirectoryInfo dirInfo = new DirectoryInfo(appPath); 
        FileInfo[] files = dirInfo.GetFiles();
        FilesGrid.DataSource = files;
        FilesGrid.DataBind();            } 
}

【问题讨论】:

  • gridview1.rows[index] 返回什么?因为看起来您正在拉取项目的 SelectedIndex 并尝试将其用作行索引。我的猜测是您需要找到 SelectedItem 所在的行,并将 GridViewRow row = 分配给它。
  • 您是否尝试过将数据重新绑定到您的网格视图?可能会丢失所有物品。
  • @Kirill:您应该提供更多信息。向我们展示 GridView ASPX 标记和数据绑定网格的代码隐藏(Page_Load?)。
  • @Kirill:我在示例应用程序中添加了一个页面并对其进行了测试。我无法重现错误。
  • @naveen:非常感谢,这让我确信这是一个本地错误,我再也不会遇到了 :)

标签: c# asp.net gridview selectedvalue selectedindexchanged


【解决方案1】:

Y 复制粘贴了您的代码,在 FilesGrid_SelectedIndexChanged 中删除此行 FilesGrid.DataBind(),我看到这是您未发布的 2 个方法,它们不在您发布 onselectedindexchange、onrowdeleting 事件、评论的代码中他们从 aspx 中查看它是否有效,或者查看该事件是否没有做一些欺骗,即删除 GridView 中的行。

告诉我它是否有效

我做了这个

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string[] dd = { "FullName" };
            FilesGrid.DataKeyNames = dd;

            string appPath = Request.PhysicalApplicationPath;
            DirectoryInfo dirInfo = new DirectoryInfo(appPath);
            FileInfo[] files = dirInfo.GetFiles();
            FilesGrid.DataSource = files;
            FilesGrid.DataBind();
        }
    }
    protected void FilesGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selected = FilesGrid.SelectedIndex; // selected = 2
        //FilesGrid.DataBind();  //added after feedback in comments. it makes no change
        int count = FilesGrid.Rows.Count; // count = 0
        GridViewRow row = FilesGrid.Rows[selected]; // row = null
        GridViewRow row0 = FilesGrid.Rows[0]; // row = null
    }
    protected void FilesGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void FilesGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }

aspx 代码。

<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true"
            AsyncPostBackTimeout="0" EnableScriptLocalization="true">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="upPanel" runat="server">
            <ContentTemplate>        
                <asp:GridView ID="FilesGrid" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True"
                    OnRowDeleting="FilesGrid_RowDeleting" OnSelectedIndexChanged="FilesGrid_SelectedIndexChanged"
                    Style="margin-top: 0px" OnSelectedIndexChanging="FilesGrid_SelectedIndexChanging">
                    <Columns>
                        <asp:CommandField ShowDeleteButton="True" />
                        <asp:BoundField DataField="Name" HeaderText="Name" />
                        <asp:BoundField DataField="Length" DataFormatString="{0:N0}" HeaderText="Size in Bytes"
                            HtmlEncode="False" />
                    </Columns>
                </asp:GridView>
            </ContentTemplate>         
        </asp:UpdatePanel>

【讨论】:

  • 您的意思是在删除(或在代码隐藏中添加空白)这些方法后,代码可以在您的机器上运行吗?不幸的是,对我来说没有什么区别。我发现在 Page_Load 中评论“if (!Page.IsPostBack)”行会有所帮助。不管怎样,谢谢你的努力。
  • 我没有评论它,是的,如果你有回发评论,这应该是原因
【解决方案2】:

删除FilesGrid.DataBind(); //在cmets反馈后添加。它没有改变 当我将该语句添加到我的代码中时,我收到了错误。删除后请重试。如果不起作用,请分享

protected void FilesGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)方法代码,可能是那个部分有问题

【讨论】:

  • 感谢您的回答。请阅读问题下方的 cmets。这个问题有另一种性质。
【解决方案3】:

通过注释掉// if (!Page.IsPostBack) 行解决了这个问题。看起来数据源在回发期间以某种方式丢失了。整体似乎是 ViewState 的一个本地 bug,因为其他用户没有观察到这种行为。我要特别感谢 Tim Schmelter 和 naveen。

【讨论】:

    【解决方案4】:

    您可以将Page.IsPostBack 保留在Page_Load 中,我遇到了同样的问题,但在我的情况下,网格不在 ViewState 中。如果您在控制器中使用 Linq 查询,您将希望将此属性添加到网格中,以便它会保持正确刷新: &lt;asp:Panel EnableViewState="True"&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多