1

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

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

4 回答 4

0

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>
于 2011-06-30T21:17:53.547 回答
0

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

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

于 2011-07-01T06:52:45.157 回答
0

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

于 2011-07-06T00:00:45.053 回答
0

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

于 2018-06-10T01:27:09.187 回答