选择行时,所有GridView行都将消失

本文关键字:消失 GridView 所有 选择 | 更新日期: 2023-09-27 18:00:54

请考虑我在调试模式下得到的注释中的值:

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();            } 
}

选择行时,所有GridView行都将消失

Y复制粘贴您的代码,在FilesGrid_SelectedIndexChanged中删除这行FilesGrid.DataBind,正在删除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>

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

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

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

您可以将Page.IsPostBack保留在Page_Load中,我遇到了同样的问题,结果发现在我的情况下,网格不在ViewState中。如果您在控制器中使用Linq查询,您将希望将此属性添加到网格中,以使其保持正常刷新:<asp:Panel EnableViewState="True">