为什么我的 GridView SelectedIndexChanged 事件没有触发?
我有一个 GridView,您可以单击一行,它应该调用 SelectedIndexChanged 方法(根据选择的行更新页面的另一部分)。我之前做过类似的事情并且它有效,但由于某种原因我似乎无法调用 SelectedIndexChanged
部分。
该页面保存在一个母版页中,该母版页具有 form runat="server"
标记和
标记,
我正在使用 e. Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.gridMessages, "Select$" & e.Row.RowIndex))
以允许 SelectedIndexChanged
通过单击行上的任意位置来触发。
为了检查代码是否确实可以工作,我添加了一个带有 SelectButton
的 CommandField
并成功触发,但我更愿意找到一个无需使用该解决方案的解决方案。
代码如下 - 任何帮助将不胜感激。谢谢
GridView
:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:HiddenField runat="server" ID="hdnScrollPosition" />
<asp:GridView ID="gridMessages" runat="server" CssClass="gridView" AutoGenerateColumns="False"
AllowPaging="true" GridLines="None" PageSize="10" ShowHeader="True"
EmptyDataText="--No Messages Received--" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Messages Received" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="headerClass">
<ItemTemplate>
....
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
代码隐藏:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.gridMessages.DataSource = ...
Me.gridMessages.DataBind()
End If
End Sub
Protected Sub gridMessages_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridMessages.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D2E6F8'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'")
e.Row.Attributes.Add("onclick", "saveScrollPosition(); " & ClientScript.GetPostBackClientHyperlink(Me.gridMessages, "Select$" & e.Row.RowIndex))
End If
End Sub
SelectedIndexChanged
(永远不会触发):
Protected Sub gridMessages_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridMessages.SelectedIndexChanged
Response.Redirect("test.aspx")
End Sub
I have a GridView
which you can click on a row and it should call the SelectedIndexChanged
method (which updates another part of the page based on which row was selected). I have done something similar to this before and it worked, but I can't seem to get the SelectedIndexChanged
part to be called for some reason.
The page is held in a master page which has a form runat="server"
tag, and an <asp:ScriptManager>
tag
I am using e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.gridMessages, "Select$" & e.Row.RowIndex))
to allow the SelectedIndexChanged
to fire by clicking anywhere on the row.
To check that the code does work apart from that, I added a CommandField
with a SelectButton
and that successfully fires, but i would prefer to find a solution without having to use that.
code is below - any help would be appreciated. Thanks
GridView
:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:HiddenField runat="server" ID="hdnScrollPosition" />
<asp:GridView ID="gridMessages" runat="server" CssClass="gridView" AutoGenerateColumns="False"
AllowPaging="true" GridLines="None" PageSize="10" ShowHeader="True"
EmptyDataText="--No Messages Received--" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Messages Received" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="headerClass">
<ItemTemplate>
....
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Code-Behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.gridMessages.DataSource = ...
Me.gridMessages.DataBind()
End If
End Sub
Protected Sub gridMessages_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridMessages.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D2E6F8'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'")
e.Row.Attributes.Add("onclick", "saveScrollPosition(); " & ClientScript.GetPostBackClientHyperlink(Me.gridMessages, "Select$" & e.Row.RowIndex))
End If
End Sub
SelectedIndexChanged
(which never fires):
Protected Sub gridMessages_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridMessages.SelectedIndexChanged
Response.Redirect("test.aspx")
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要将
CommandField
列定义为SelectButton
吗?然后,您的标记将类似于:您没有询问这一点,但我总是觉得有必要提及诸如这两行之类的事情:
这是代码味道。这并不是一个坏的习惯,但是将 JavaScript 属性与 VB/C# 代码混合在一起是您现在应该改掉的一种习惯。如果您需要执行类似的操作,请向 GridView 添加一个
CssClass
属性,并在 CSS 中定义这些操作(如果 CSS 没有足够的事件,则在 JavaScript/jQuery 中定义这些操作)。编辑:
根据我们在评论中的讨论,这看起来与 GridView 的修改方式不一致。它可能与页面/事件生命周期有关(不知何故,ASP.NET 正确连接事件为时已晚?),这里最好的选择是从
SelectedIndexChanged
事件切换到选定索引更改
。Don't you need a column
CommandField
defined as aSelectButton
? Then, your markup would look something like:You didn't ask about this, but I always feel compelled to mention things like these two lines:
This is a code smell. It's not a bad one, but mixing JavaScript attributes with VB/C# code is a habit you should break out of now. If you need to do something like this, add a
CssClass
property to your GridView and define those actions in CSS (or JavaScript/jQuery if CSS doesn't have enough events for you).Edit:
Based on our discussion in comments, this looks like an inconsistency with the way the GridView can be modified. It may be related to the page/event lifecycle (somehow it's too late for ASP.NET to properly hook up the events?), and your best bet here is to switch from the
SelectedIndexChanged
event toSelectedIndexChanging
.我一直在努力解决同样的问题...
它可能不适用于您的场景(或者根本不是一件好事),但请尝试为页面设置
EnableEventValidation="false"
。这对我来说就是不同之处。如上所述,使用SelectedIndexChanged
或SelectedIndexChanging
事件进行工作。I've been wrestling with the same issue...
It may not work in your scenario (or be a good thing to do at all) but try setting
EnableEventValidation="false"
for the page. This was the difference for me. Worked using eitherSelectedIndexChanged
orSelectedIndexChanging
events as mentioned above.使用这个:
Use this: