为什么当调用一个Asp.Net列表框自动回法无关(自动回)方法被调用? [英] Why do unrelated (autopostback) methods get called when an Asp.Net ListBox autopostback method is invoked?

查看:184
本文介绍了为什么当调用一个Asp.Net列表框自动回法无关(自动回)方法被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,这是简单,但它的驾驶我疯了。

I'm sure this is simple but it's driving me nuts.

我有我的页面上的列表框显示的艺术家,当索引改变它调用的方法,并点击它时,从另一个页面加载列表艺术家的按钮:

I have a ListBox on my page to show artists, which calls a method when the index is changed, and a button which loads an artist from that list in another page when clicked:

<asp:ListBox ID="lbArtists" runat="server" Rows="1" AutoPostBack="true" OnSelectedIndexChanged="ShowArtistsWorks" />

<asp:Button ID="btnEditArtist" runat="server" Text="Edit the artist" OnClick="LoadArtist" />

另外,我有一个类似的链接列表,其中也有一个自动回发方法:

Further on, I have a similar list of links, which also has an autopostback method:

<asp:ListBox ID="lbLinks" runat="server" Rows="1" AutoPostBack="true" OnSelectedIndexChanged="LoadLink" />

问题是,当我调用 ShowArtistsWorks()点击 btnEditArtist LoadLink()方法也被调用。这是为什么发生?为什么当我并没有改变对 lbLinks ListBox中的索引,它被调用?它不应该去接近这个方法。

The problem is, when I invoke ShowArtistsWorks() by clicking btnEditArtist, the LoadLink() method also gets called. Why is that happening? Why would that get called when I haven't changed the index on the lbLinks ListBox? It shouldn't be going near that method.

编辑:(相关)code-背后的方法(

(relevant) Code-behind methods (

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack){
        GetArtists(); // populates artists listbox
        GetLinks(); // populates links listbox
    }
}

protected void LoadArtist(object sender, EventArgs e){
    if (lbArtists.SelectedValue != "")
        Response.Redirect("Artist.aspx?id=" + lbArtists.SelectedValue);
}

protected void LoadLink(object sender, EventArgs e)
{
    if (lbLinks.SelectedValue != "")
        Response.Redirect("Link.aspx?id=" + lbLinks.SelectedValue);
}

编辑#2:我可以很容易杂牌此修复程序在各个方法阻止他们发生时,他们不应该,但我想知道为什么的方法我不叫,而只能从一个地方叫,无意间得到调用。

EDIT #2: I could easily kludge a fix for this in the individual methods to stop them happening when they shouldn't, but I want to understand why methods that I don't call, and that only get called from one place, get invoked inadvertently.

接受的答案:虽然恩赐(现CRice)首先与解释和解决办法了,我决定接受杰夫的更透彻的解释,因为这是我想要的东西,更深入分析。感谢大家谁回答。

ACCEPTED ANSWER: Even though Boon (now CRice) got in first with an explanation and a solution, I decided to accept Jeff's more thorough explanation because that was what I wanted, a more in-depth analysis. Thanks to everyone who answered.

推荐答案

更改事件被触发每个回发的,他们是相关的 - 作为MSDN主题中所述的 ASP.NET Web服务器控件事件模型

Change events are raised on every postback for which they're relevant - as described in the MSDN topic "ASP.NET Web Server Control Event Model."

更改事件
  和Web服务器控件,如
  TextBox控件,不要立即
  导致一个职位。相反,他们提出
  下一次发生后

当用户单击编辑艺术家按钮,ASP.NET认为 lbLinks.SelectedIndex 已经改变,所以它调用其的SelectedIndexChanged 处理程序。

When users click your 'Edit Artist' button, ASP.NET thinks lbLinks.SelectedIndex has changed, so it invokes its SelectedIndexChanged handler.

的原因的ASP.NET认为改变了指数是这样的:当第一次加载页面, lbLinks 没有所选索引(或价值),除非你通过显式设置它,否则说。直到你做到这一点,选择指数为-1,其选择的值是一个空字符串。所选的值(在这种情况下,一个空字符串)写入到视图状态时,呈现页面的使ASP.NET可以告诉当值上回发改

The reason ASP.NET thinks the index has changed is this: when the page first loads, lbLinks doesn't have a selected index (or value) unless you say otherwise by explicitly setting it. Until you do that, the selected index is -1 and its selected value is an empty string. The selected value (in this case, an empty string) is written to view state when the page is rendered so that ASP.NET can tell if the value has changed on postbacks.

您可以通过渲染之前检查你的列表框中选择指标和价值观遵守本在调试时,也可以使用在线视图状态去codeRS之一(的像这样的),看看在你的网页它的第一次写入时(虽然阅读本你需要了解序列化视图状态数据的结构)。

You can observe this while debugging by inspecting your list boxes' selected indices and values before rendering, or you can use one of the online view state decoders (like this one) to see what's in your page when it's first written (though to read this, you need to know about the structure of serialized view state data).

在你的下一篇文章后面,HTML &LT;选择&GT; 元素 lbLinks 具有非空值的,并且它提交的POST数据的一部分。看看的Request.Form [lbLinks] ,你会看到,它等于 lbLinks.Items [0] .value的

When you next post back, the HTML <select> element lbLinks has a non-empty value, and it is submitted as part of the post data. Take a look at Request.Form["lbLinks"] and you'll see that it equals lbLinks.Items[0].Value.

ASP.NET张贴的值映射到 lbLinks.SelectedValue ,但它也知道所选值的用于为空字符串的 - 它会从视图状态中的旧值。由于这两个值是不同的,这个过程提高了控制的选定索引更改事件,造成你观察到的不良行为。

ASP.NET maps the posted value to lbLinks.SelectedValue, but it also knows that the selected value used to be an empty string - it gets the old value from view state. Since the two values are different, the process raises the control's selected index changed event, causing the undesirable behavior you've observed.

<一个href=\"http://stackoverflow.com/questions/1733077/why-do-unrelated-autopostback-methods-get-called-when-an-asp-net-listbox-autopo/1752797#1752797\">As恩建议,解决的办法是要始终明确设置的SelectedIndex 您所有的的ListBox 控件,当你重新使用 OnSelectedIndexChanged 时,即使你的指标只是设置为零。

As boon suggested, the solution is to always explicitly set the SelectedIndex for all your ListBox controls when you're using the OnSelectedIndexChanged event, even if you're just setting the index to zero.

(将AutoPostBack设置是不相关的红鲱鱼。如果你从两个列表框中删除它,它们的 OnSelectedIndexChanged 事件都将火灾每次单击按钮。)

(The AutoPostBack setting is an unrelated red herring. If you remove it from both list boxes, their OnSelectedIndexChanged events will both fire every time you click the button.)

这篇关于为什么当调用一个Asp.Net列表框自动回法无关(自动回)方法被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆