简体   繁体   中英

No browser postback on dropdownlist after upgrading from .NET framework 3.5 to 4.0

I have a simple ASP.NET dropdownlist control in a visual studio project (.NET framework 3.5) that triggers a postback once the selected Index is changed and it posts back to the server successfully. Taking the same simple ASP.NET dropdownlist control and upgrading the visual studio project to the .NET framework 4.0 results in no postback. Below is a sample of the markup on the Page (Page only contains a dropdownlist)

<body>
  <form id="form1" runat="server" >
    <asp:DropDownList ID="ddlTest" OnSelectedIndexChanged="ddlTestIndexChanged" runat="server"
     AutoPostBack="true" />
 </form> 
</body>

And in the code behind....

public void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var list = new List<string>();
                list.Add("Hello");
                list.Add("World");
                ddlTest.DataSource = list;
                ddlTest.DataBind();
            } 
        }
        protected void ddlTestIndexChanged(object sender, EventArgs e)
        {  
            //selected index changed...success
        }

Viewing the rendered html in the browser using FireBug shows the following html when the project is run on .NET Framework 3.5

<body>
 <form name="form1" method="post" action="Default.aspx" id="form1">
 <div>
 <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="">
 <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="">
 <input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="">
 <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="has a value">
</div>
 <script type="text/javascript"> //<![CDATA[ var theForm = document.forms['form1']; 
    if (!theForm) { theForm = document.form1; } function __doPostBack(eventTarget, eventArgument)
     { if (!theForm.onsubmit || (theForm.onsubmit() != false)) 
    { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; 
    theForm.submit(); } } //]]> </script>
 <div>
 <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="Has a value">
</div>
 <select name="ddlTest" onchange="javascript:setTimeout('__doPostBack(\'ddlTest\',\'\')', 0)" id="ddlTest">
 <option value="Hello">Hello</option>
 <option selected="selected" value="World">World</option>
</select>
</form>
</body>

And viewing the rendered html when the same project is run on the .NET framework 4.0

<body>
 <form name="form1" method="post" action="/Default.aspx" id="form1">
 <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="has a value">
 <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="has a value">
 <select name="ddlTest" id="ddlTest">
 <option selected="selected" value="Hello">Hello</option>
 <option value="World">World</option>
</select>
</form>
</body>

From comparing the html code in both snippets, it can be seen that the latter snippet is missing the “onchange” event on the dropdownlist along with the javascript itself to do the postback. My web.config contains the following for backward compatibility but this does not seen to be working either.

<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>

I have also tried adding the missing Javascript in the markup – didn't work. Also, tried adding the onchange event + javascript from the code behind but that didn't work either

Environment:

  • Adobe AIR 3.2 based browser (no access to browser code)
  • Windows 7 – 64 bit machine
  • Visual studio 2010
  • C#

I am aware that this issue is not a problem on the majority of browsers however as part of a work project this needs to work with an Adobe AIR 3.2 based browser. Any help is greatly appreciated, Thanks!

I copied your ASPX and ASPX.cs on my local machine and it's working fine with .NET framework 4.0. I can see dropdown has onchange event.

<select id="ddlTest" onchange="javascript:setTimeout('__doPostBack(\'ddlTest\',\'\')', 0)" name="ddlTest">
    <option value="Hello" selected="selected">Hello</option>
    <option value="World">World</option>

</select>

Check your web.config and see what is the targetFramework:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

</configuration>

The solution was to add an "App_Browsers" folder to my Website project. Then add an "AdobeAir.Browser" file. This is an xml file containing the attributes of my custom browser. Please see http://msdn.microsoft.com/en-us/library/ms228122.aspx for more information.

.NET framework 4 browser definition files located at C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\Config\\Browsers are a little light compared to those found at C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\CONFIG\\Browsers hence my custom browser worked on pre .NET framework 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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