为什么我的 GridView SelectedIndexChanged 事件没有触发?

发布于 2024-12-19 14:14:45 字数 2676 浏览 5 评论 0原文

我有一个 GridView,您可以单击一行,它应该调用 SelectedIndexChanged 方法(根据选择的行更新页面的另一部分)。我之前做过类似的事情并且它有效,但由于某种原因我似乎无法调用 SelectedIndexChanged 部分。

该页面保存在一个母版页中,该母版页具有 form runat="server" 标记和 标记,

我正在使用 e. Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.gridMessages, "Select$" & e.Row.RowIndex)) 以允许 SelectedIndexChanged通过单击行上的任意位置来触发。

为了检查代码是否确实可以工作,我添加了一个带有 SelectButtonCommandField 并成功触发,但我更愿意找到一个无需使用该解决方案的解决方案。

代码如下 - 任何帮助将不胜感激。谢谢

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的温暖 2024-12-26 14:14:45

您不需要将 CommandField 列定义为 SelectButton 吗?然后,您的标记将类似于:

<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:CommandField ShowSelectButton="true" ButtonType="Button" />
                <asp:TemplateField HeaderText="Messages Received" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="headerClass">
                    <ItemTemplate>
                        ....
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

您没有询问这一点,但我总是觉得有必要提及诸如这两行之类的事情:

e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D2E6F8'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'")

这是代码味道。这并不是一个的习惯,但是将 JavaScript 属性与 VB/C# 代码混合在一起是您现在应该改掉的一种习惯。如果您需要执行类似的操作,请向 GridView 添加一个 CssClass 属性,并在 CSS 中定义这些操作(如果 CSS 没有足够的事件,则在 JavaScript/jQuery 中定义这些操作)。


编辑:

根据我们在评论中的讨论,这看起来与 GridView 的修改方式不一致。它可能与页面/事件生命周期有关(不知何故,ASP.NET 正确连接事件为时已晚?),这里最好的选择是从 SelectedIndexChanged 事件切换到 选定索引更改

Don't you need a column CommandField defined as a SelectButton? Then, your markup would look something like:

<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:CommandField ShowSelectButton="true" ButtonType="Button" />
                <asp:TemplateField HeaderText="Messages Received" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="headerClass">
                    <ItemTemplate>
                        ....
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

You didn't ask about this, but I always feel compelled to mention things like these two lines:

e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D2E6F8'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'")

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 to SelectedIndexChanging.

鲜肉鲜肉永远不皱 2024-12-26 14:14:45

我一直在努力解决同样的问题...

它可能不适用于您的场景(或者根本不是一件好事),但请尝试为页面设置 EnableEventValidation="false" 。这对我来说就是不同之处。如上所述,使用 SelectedIndexChangedSelectedIndexChanging 事件进行工作。

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 either SelectedIndexChanged or SelectedIndexChanging events as mentioned above.

傾城如夢未必闌珊 2024-12-26 14:14:45

使用这个:

e.Row.Attributes["onclick"]=ClientScript.GetPostBackEventReference(this.grvDetails,"Select$"+e.Row.RowIndex.ToString());

Use this:

e.Row.Attributes["onclick"]=ClientScript.GetPostBackEventReference(this.grvDetails,"Select$"+e.Row.RowIndex.ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文