简体   繁体   English

即使设置了AutoPostBack,DropDownList也不会回发

[英]DropDownList not posting back, even with AutoPostBack set

Obscure solution at end of post 在帖子结束时隐藏的解决方案

Using asp.net page with C# codebehind, I have successfully built and populated a DropDownList. 使用带有C#codebehind的asp.net页面,我已经成功构建并填充了DropDownList。

What I would like to do is capture a new value selected from the dropdownlist (preferrably using a postback to my codebehind). 我想要做的是捕获从下拉列表中选择的新值(最好使用回发到我的代码隐藏)。 The codebehind can then update other things on the page based on this newly selected dropdownlist value. 然后,代码隐藏可以根据这个新选择的下拉列表值更新页面上的其他内容。

My first attempt was to use 我的第一次尝试是使用

<asp:DropDownList ID="myDDL" runat="server" AutoPostBack="true" OnSelectedIndexChanged="foo"></asp:DropDownList>

with the C# method 使用C#方法

public void foo(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;
    string myValue = "";

    if (ddl != null)
    {
        myValue = ddl.SelectedValue;
        // Do some stuff
    }
}

This did not work. 这没用。 When the selected index was changed, it simply reloaded the page, but the IsPostBack flag was always false. 当所选索引发生更改时,它只是重新加载页面,但IsPostBack标志始终为false。

So I sifted through SO and tried a number of different tactics. 所以我筛选了SO并尝试了许多不同的策略。 Most recently, I tried to register the client-side onChange event in the codebehind and turned off the AutoPostBack. 最近,我尝试在代码隐藏中注册客户端onChange事件并关闭AutoPostBack。

in the ASP.Net page: 在ASP.Net页面中:

<asp:DropDownList ID="myDDL" runat="server" AutoPostBack="false"></asp:DropDownList>

in the codebehind: 在代码隐藏中:

myDDL.Attributes.Add("onChange", "doSomeStuff(this);"); // Done on databind.

I added the client-side javascript to call the page's __doPostBack function 我添加了客户端javascript来调用页面的__doPostBack函数

<script language="javascript" type="text/javascript">
    function doSomeStuff(ddl) {
        var ddlVals = document.getElementById(ddl.id);

        __doPostBack(ddlVals, '');
    }
</script>

This also failed, although I thought it was going somewhere when I saw the javascript execute properly. 这也失败了,虽然我认为当我看到javascript正确执行时它正在某个地方。

Looking in the codebehind, though, it's still not working. 然而,看一下代码隐藏,它仍然没有用。 When I put a breakpoint in the Page_Load IsPostBack is false! 当我在Page_Load中放置一个断点IsPostBack是假的! But it's supposed to be a postback!? 但它应该是一个回发!? It was posted back using __doPostBack and (seperately) automatically with AutoPostBack="true" 使用__doPostBack和(单独)使用AutoPostBack =“true”自动回发

So I dug deeper. 所以我挖得更深。

According to this MSDN article (http://msdn.microsoft.com/en-us/library/ms178141(v=VS.85).aspx) based on the results on the page load I'm doing a "Server Transfer" rather than the desired Postback (IsPostBack is false, PreviousPage is, as expected, the same page that should be posting back, IsCallback is false, and IsCrossPagePosting is false). 根据这篇MSDN文章(http://msdn.microsoft.com/en-us/library/ms178141(v=VS.85).aspx)根据页面加载的结果我正在做“服务器传输”而不是所需的Postback(IsPostBack为false,正如预期的那样,PreviousPage是应回发的同一页面,IsCallback为false,IsCrossPagePosting为false)。

What could be going on to hyjack the AutoPostBack and the __doPostBack to make it look and act like a "Server Transfer"? 有什么可以继续劫持AutoPostBack和__doPostBack使它看起来像一个“服务器转移”?

What can I set/check on the parent control /page to make sure it's allowing postbacks? 我可以在父控件 /页面上设置/检查什么以确保它允许回发?

EDIT: 编辑:

The Page_Load looks something like: Page_Load看起来像:

private SpecialDataObject _someData;
private string foobar;
public void Page_Load(object sender, EventArgs e)
{
    //set some variables.
    this.foobar = "blah";

    LoadSomeUnrelatedData();

    if (!IsPostBack)
    {
        if (_someData == null)
        {
            LoadDataWithoutBinding();
        }
        BindMyData();
    }
}

With a breakpoint at //set some variables the Page.IsPostBack is always false even after AutoPostBack. 使用断点//set some variables ,即使在AutoPostBack之后,Page.IsPostBack也始终为false。

EDIT 2: 编辑2:

The answer was in the Server Transfer. 答案是在服务器转移中。 In a distant control loaded from the master page, the URL is inspected and rerouted before it hits the page, effectively negating my postback. 在从母版页加载的远程控件中,URL会在到达页面之前进行检查和重新路由,从而有效地否定了我的回发。 I didn't see it before because I had added the breakpoints only in the target page. 之前我没有看到它,因为我只在目标页面中添加了断点。

I would check to make sure that you don't have validation somewhere interfering with the postback. 我会检查以确保您没有在某处干扰回发的验证。 To check this, set CausesValidation to false on the DropDownList. 要检查这一点,请在DropDownList上将CausesValidation设置为false。

Are you resetting the value of your dropdown in your PageLoad? 您是否重置了PageLoad中下拉列表的值?

Also you may want to think about using an UpdatePanel so that it doesnt reload the whole page. 您也可以考虑使用UpdatePanel,以便它不会重新加载整个页面。

Is it inside an UpdatePanel? 它在UpdatePanel中吗? If yes, set ChildrenAsTriggers="true" 如果是,请设置ChildrenAsTriggers =“true”

Based on the attempts you've mentioned, as well as the comments regarding the update panel , I attempted a few things. 根据您提到的尝试以及有关更新面板的评论 ,我尝试了一些方法。

By setting the data source on the load event, you will only do it once: 通过在load事件上设置数据源,您只需执行一次:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //set up data here
    }
}

You may use the Page.IsPostBack code and the method of yours to get what you want: 您可以使用Page.IsPostBack代码和您的方法来获得您想要的内容:

if (Page.IsPostBack)
{
    //do page reload logic in here
}

protected void foo(object sender, EventArgs e)
{
    //get your selected value here
}

(Note: Both of the postback conditionals are in the page load event) (注意:两个回发条件都在页面加载事件中)

EDIT: 编辑:

Here's the whole setup, its basic, but you get the idea: 这是整个设置,它的基本,但你明白了:

在此输入图像描述

As you can see when I made a selection from cat to dog, it recognized that there was a postback, so it skipped the databind and set t. 正如你所看到的那样,当我从一只猫到另一只狗做出选择时,它认识到有一个回发,所以它跳过了数据绑定并设置了t。 I could only assume there's something else here that I'm not seeing if you can't get it to ever return true for you on postback. 我只能假设这里还有别的东西,如果你不能让它在回发中为你回归真实,我就没有看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM