请考虑我在调试模式中获得的注释中的值:
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(); }
}
答案 0 :(得分:0)
Y复制粘贴你的代码,删除FilesGrid_SelectedIndexChanged中的这行FilesGrid.DataBind(),我看到你没有发布的2个方法,这些方法不在你发布的onselectedindexchanging,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>
答案 1 :(得分:0)
删除评论中反馈后添加的FilesGrid.DataBind();
//。它没有变化
当我将该语句添加到我的代码中时,我收到错误。删除后请再试一次。如果不能正常工作,请分享
protected void FilesGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
方法代码可能是该部分的错误
答案 2 :(得分:0)
通过评论专栏// if (!Page.IsPostBack)
解决了该问题。看起来数据源在回发期间以某种方式丢失。整个似乎是ViewState的本地错误,因为其他用户没有观察到这种行为。我特别感谢Tim Schmelter和naveen。
答案 3 :(得分:0)
您可以将Page.IsPostBack
保留在Page_Load
中,我遇到了同样的问题,结果发现网格在我的情况下不在ViewState中。如果在控制器中使用Linq查询,则需要将此属性添加到网格中,以便正确刷新:
<asp:Panel EnableViewState="True">