【问题标题】:Dropdownlist autopostback property is getting triggered even when I click any other options in gridview or on any other control on the same page即使我单击 gridview 或同一页面上的任何其他控件中的任何其他选项,下拉列表自动回发属性也会被触发
【发布时间】:2017-09-17 17:23:46
【问题描述】:

下拉列表和网格视图在同一页面上。

下拉列表代码:-

    <asp:DropDownList OnSelectedIndexChanged="drplist_SelectedIndexChanged" 
    AutoPostBack="true" ViewStateMode="Disabled" AppendDataBoundItems="true" 
    ID="drplist" runat="server" DataSourceID="datasource1" 
    DataTextField="User_ID" DataValueField="User_ID">
        <asp:ListItem Value="" Text="Make a selection"/>
    </asp:DropDownList>  

gridview 的代码:-

    <asp:GridView ID="gview" runat="server" Width="100%"  AutoGenerateColumns="false" DataKeyNames="User_ID" DataSourceID="datasource">
        <Columns>
            <asp:BoundField DataField="User_ID" HeaderText="User ID" />
            <asp:BoundField DataField="User_Name" HeaderText="User Name" />
            <asp:BoundField DataField="User_Email" HeaderText="User e-mail" />
            <asp:BoundField DataField="User_Mob" HeaderText="User mobile number" />
            <asp:BoundField DataField="User_Created" HeaderText="Created Date" />
            <asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />
        </Columns>
    </asp:GridView>

下拉列表具有来自数据库的用户 ID,当从下拉列表中进行选择时,它会触发 selectedIndexChanged 事件,相同的代码:-

    protected void drplist_SelectedIndexChanged(object sender, EventArgs e)
    {
        var index = drplist.SelectedValue;

        drplist.SelectedIndex = 0;
        Response.Redirect("~/Demo/TableDemo.aspx?User_ID="+index);

    }

现在,当我单击浏览器的后退按钮时,下拉列表仍保留其选定的值,我不想要,我希望它重置为索引 0。当我从 TableDemo.aspx 页面并单击 gridview 中的编辑按钮或删除按钮,再次触发下拉的回发,将我带到 TableDemo.aspx 页面。只有手动页面刷新才能使事情变得简单。我尝试搜索并实现 JS 代码以在按下浏览器后退按钮时强制页面刷新,但它并没有解决我的问题并创建了更多。我曾尝试使用更新面板让我很沮丧,但它碰巧没有解决我的问题。很抱歉,我无法在此处写出更短的场景来做得更好。

【问题讨论】:

    标签: asp.net webforms


    【解决方案1】:

    您可能已经缓存了您的页面,这就是您单击后退按钮时它的行为不符合您想要的方式的原因。尝试将此代码添加到您不想被缓存的页面上的 page_load。

    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    

    希望这会有所帮助!

    【讨论】:

    • 嗨@chrillelundmark,感谢您分享您的回复。我在有下拉菜单的页面的 Page_Load 方法中添加了上述行,但它似乎没有帮助。
    猜你喜欢
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多