Asp.net button doesn't submits disabled controls, even with submitdisabledcontrols="true" - asp.net

I have a page with some disabled controls, it looks like this
<form id="form1" runat="server" submitdisabledcontrols="true">
<asp:UpdatePanel ID="upp" runat="server">
<ContentTemplate>
<asp:TextBox ID="textbox1" runat="server" AutoPostBack="True" ontextchanged="textchaged_handler" />
<asp:TextBox ID="textbox2" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:ImageButton ID="ibVerify" runat="server" OnClick="btnVerify_Click" ImageUrl="~/img/imagebutton.png" AlternateText="Verify" />
</form>
Programatically, the second textbox is disabled with some server side code, during initialization.
The problem is that, even setting submitdisabledcontrols="true" in the form tag, the disabled textbox value isn't submited to the server when I click the ImageButton. I checked this with firebug, and also in VS, where the old value is retrieved.
When I press TAB in the first textbox, however, the second textbox value gets posted, no matter submitdisabledcontrols is set or not...
Any ideas?

That form attribute states that "Gets or sets a Boolean value indicating whether to force controls disabled on the client to submit their values.", so I think you need to use the client-side disabled property, not the server-side ENabled property, as in:
<asp:TextBox .. disabled="disabled" />

Instead of
control.Enabled = false;
Use
control.Attributes.Add("disabled", "disabled");
Thus from an asp .net perspective the control is still enabled, but it is rendered client-side as a disabled control.

In case any one else comes upon this page like I did, I found that the reason that "submitdisabledcontrols = true" was not working was because of my page doctype:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
when I changed this to:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
then it worked.
Hope this helps someone :)

Related

ViewState is not required to preserve control values so what does it do?

I have a very simply web page with ViewState disabled everywhere:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" EnableViewState="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" EnableViewState="false"></asp:TextBox>
<asp:DropDownList runat="server" id="mylist" EnableViewState="false">
<asp:ListItem>my item 1</asp:ListItem>
<asp:ListItem>my item 2</asp:ListItem>
<asp:ListItem>my item 3</asp:ListItem>
<asp:ListItem>my item 4</asp:ListItem>
<asp:ListItem>my item 5</asp:ListItem>
<asp:ListItem>my item 6</asp:ListItem>
</asp:DropDownList>
<asp:Button runat="server" Text="click me"/>
</div>
</form>
</body>
</html>
Code behind
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Dropdown list value is " + mylist.SelectedValue);
}
}
As you can see, no viewstate is enabled but it does preserve controls values, see here for the running example http://www.yart.com.au/stackoverflow/viewstate/test.aspx
Edit
latr0dectus has somewhat answered my question below. But what's an example where ViewState is required practically? I can't see what you need from the form other than control values.
#Petras: ViewState is not required to preserve control values so what does it
do?
Controls that implements IPostBackDataHandler uses LoadPostData() method to assign to some properties.
Read this article : Understanding ASP.NET View State
It is a common misconception among developers that view state is
somehow responsible for having TextBoxes, CheckBoxes, DropDownLists,
and other Web controls remember their values across postback. This is
not the case, as the values are identified via posted back form field
values, and assigned in the LoadPostData() method for those controls
that implement IPostBackDataHandler.
I'm not really sure what your question is.
View state is used in the page lifecycle. After the page is served it is destroyed on the server. Then the browser posts back it also posts back the viewstate. The server can use this in combination with the posted form values to recreate the previous state of the page and then show the changes.
In some cases even with viewstate disabled certain controls will appear to work as if they have viewstate enabled. This is because some controls have what is called "Control State". It operates almost like viewstate, except it cannot be disabled. This is because some controls would cease to function properly without it.
In the example you posted I think you are observing that the selected value of the dropdown is being posted to the server during postback. Not that it was reconstructed from viewstate.
Im adding this information that I found from the following link:
http://aspnetresources.com/articles/ViewState
What's the moral of this story? You don't always need view state enabled to maintain page state. "When do I need it though? What's it for then?" Glad you asked. The prime candidates for participation in view state are those controls that don't post back with the HTTP form and controls added or populated dynamically.
Scan to that part of the document and you should find what you are looking for.

combobox items not displaying

This question has been asked here before, but the author wasn't very clear and got no answers (1st ref). Why aren't my combobox items displaying correctly? It is almost like they are there, but not connected to the combobox (notice my label, it shows up halfway down the page instead of right underneath the combobox when I run it). I get no errors on the page, which is good (different than when I tried doing this in VS2005), and the combobox displays fine, but when I click it, nothing is there.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:ComboBox ID="ComboBox1" runat="server" AutoPostBack="true"
DropDownStyle="DropDownList">
<asp:ListItem>thisis</asp:ListItem>
<asp:ListItem>mynetwork</asp:ListItem>
<asp:ListItem>getoffmyjunk</asp:ListItem>
<asp:ListItem>itsmine</asp:ListItem>
<asp:ListItem>illkillyou</asp:ListItem>
<asp:ListItem>forrealz</asp:ListItem>
<asp:ListItem>what</asp:ListItem>
<asp:ListItem>didyousaysomething</asp:ListItem>
<asp:ListItem>didn'tthinkso</asp:ListItem>
<asp:ListItem>meh</asp:ListItem>
</asp:ComboBox>
<br />
<asp:Label ID="lbl" runat="server">default text</asp:Label>
</div>
</form>
</body>
</html>
https://stackoverflow.com/questions/4322922/combo-box-items-does-not-display-below-of-combo-box-why
I'm having the same problem as this guy:
http://p2p.wrox.com/book-asp-net-ajax-programmers-reference-asp-net-2-0-asp-net-3-5-isbn-978-0-470-10998-4/80267-ajax-combobox-not-displaying-item-list-correctly.html
Your syntax looks to be incorrect for the ComboBox control. You want to use the Text and Value parameters in order to get the options to display. You'll want it to look like this:
<asp:ComboBox ID="ComboBox1" runat="server" AutoPostBack="true" DropDownStyle="DropDownList">
<asp:ListItem Text="thisis" Value="thisis"></asp:ListItem>
<asp:ListItem Text="mynetwork" Value="mynetwork"></asp:ListItem>
<asp:ListItem Text="getoffmyjunk" Value="getoffmyjunk"></asp:ListItem>
</asp:ComboBox>
Note: You may also be running into issues with your control tag declaration. For example, when I import the AJAX Control Toolkit into my applications, and add the proper references, I wind up using the tag "cc1" for all of the AJAX Control Toolkit controls. You may need to double check these references as well.
Use toolkitscriptmanager xDwanted to give the answer to Tim, but he won't put his comment as an answer ;;

asp net 4 - autopostback doesnt fire in ie6

OK, Im really stumped and hoping this is something simple. I have a form that relies on an autopostback of a radiobuttonlist to show or hide something. This was really elaborate at first and working fine, until I tested in IE6. The code below is as basic as I can get, all my code behind does is update the label to the radiobutton's selected value on click. This works in IE7 and 8, but not in IE6, what gives?
<%# Page Title="" Language="vb" AutoEventWireup="false" CodeBehind="testpostback.aspx.vb" Inherits="Checkout.testpostback" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:RadioButtonList ID="radio1" runat="server" AutoPostBack="true">
<asp:ListItem Text="Check1" Value="Check1" />
<asp:ListItem Text="Check2" Value="Check2" />
</asp:RadioButtonList>
<asp:Label ID="label1" runat="server" Text="none" />
</form>
</body>
</html>
Take a look at the generated javascript in your web page and probably it uses new features of javascript that couldn't be executed by IE6.
Chances are you can debug the javascript and see what happens.
Consider that IE6 in XP Sp3 is not the same as IE6 in earlier XPs and it has less problems.
This came up in this question as well. It seems to be an IE6 bug.

ASP .NET - CollapsiblePanelExtender does not work in IE7

I am running the ASP .NET AJAX Toolkit 3.5.
I have setup a panel with a collapsablePanelExtender and it works in Firefox 3.5 but not in IE7! In IE7 all the "collapsed" panels never shrink - activating the button does nothing.
My code:
<asp:ImageButton ID="btnA" runat="server" ImageUrl="~/Image/expand.gif" />
<asp:Panel ID="pnlA" runat="server" >
<!-- grid -->
<asp:GridView ID="gridA" runat="server"
AllowPaging="True" AllowSorting="True"
DataSourceID="sdsA" GridLines="Vertical">
</asp:GridView>
</asp:Panel>
<cc1:CollapsiblePanelExtender ID="cpeA" runat="server"
Enabled="True" TargetControlID="pnlA"
CollapsedSize="0" ExpandedSize="300" Collapsed="true" ScrollContents="true"
ExpandControlID="btnA" CollapseControlID="btnA"
ExpandDirection="Vertical" ExpandedImage="~/Image/collapse.gif"
CollapsedImage="~/Image/expand.gif"
ImageControlID="btnA" AutoExpand="false" SuppressPostBack="true">
</cc1:CollapsiblePanelExtender>
Is there something wrong with the code?
The DOCTYPE I have been using is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
I also tried:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
UPDATE1:
It looks as though clicking on the button resulting in the panel 'hiding' for a brief moment before it re-appears.
I just checked the code in IE8, Firefox and Chrome and it seems to be working with all of them. Just check it out on IE8, and let me know if it works.
Either IE7 settings, or the browser itself is at fault. Code is just fine.
I'll provide an additional answer in case anyone else encounters this:
A possible alternative is to use the accordion control. This works well in IE7.

ASP.NET Compile behavior changes depending on page content?

I've been struggling with some issues relating to referencing child controls within a FormView. Another developer wrote an ASPX page that is compiling and working and in that code, he references child controls within a FormView directly as properties of the page object. The page is part of an ASP.NET Web SITE Project (as opposed to a Web Application Project). We decided to convert the project to the Web Application Project model and noticed that these property references now don't compile. The code behind file does not generate the controls within the form view.
While researching that issue (I had a separate post here regarding those problems), I came across something baffling. From all the posts I've read, you should always need to reference child controls within a FormView's template using FindControl -- i.e. it is supposedly not possible to do through a simple generated property regardless of whether you're in the Web Site Project model or the Web Application Project model.
I was puzzled as to how my colleague's code compiled and ran. As I indicated, he is referencing the FormView's contained child controls through simple properties in the page and did not have to resort to FindControl calls. So to get to the bottom of this mystery, I cooked up the shortest example that demonstrates this phenomenon.
What I found was very strange. The code I have here has a ASP:FormView with a few label controls within it's ItemTemplate. One of those labels has the ID MyComment. When the FormView databinds (to the Northwind's Products table), I simply set some text.
using System;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.FormView1.ChangeMode(FormViewMode.ReadOnly);
}
protected void FormView1_DataBound(object sender, EventArgs e) {
MyComment.Text = "Data bound at " + DateTime.Now.ToString();
}
}
This code will not compile because MyComment is not a valid property. Here comes the strange part. If I embed within the FormView's ItemTemplate a TabContainer control from the Ajax Control Toolkit library, the code above does compile and runs correctly.
So the reason my colleague's code is compiling is because of the embedded TabContainer control within the FormView?? Why this should change the behavior of the compiler and the mechanisms by which you can get access to the FormView's child controls is a mystery to me. Incidentally, despite the fact that it compiles cleanly and runs correctly, Intellisense does not see these properties and ReSharper reports them as compile errors (by the red light in the indicator bar).
Here is the markup for the page. Can anyone shed some light on this behavior? Incidentally, I'm not complaining about the fact that ASP.NET creates these properties in this circumstance. (Unfortunately, this happy, but strange, behavior, only seems to apply if the project is a Web Site Project; as a Web Application Project, property accessors don't work within the FormView even with the embedded TabControl).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ProductID" DataSourceID="SqlDataSource1"
OnDataBound="FormView1_DataBound">
<ItemTemplate>
<ajaxToolkit:TabContainer runat="server" ID="TabsItem">
<ajaxToolkit:TabPanel runat="Server" ID="PanelBasicsItem" HeaderText="Basics">
<ContentTemplate>
ProductID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
<br />
ProductName:
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Bind("ProductName") %>' />
<br />
My Comment:
<asp:Label ID="MyComment" runat="server"></asp:Label>
<br />
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName] FROM [Alphabetical list of products]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
When you converted the Web site to a web application, did you check that the partial classes were correctly generated for your aspx files (e.g. there should be a file called "Default.aspx.cs.designer" in your web application, under the Default.aspx and Default.aspx.cs files).
In a web site, these are generated on the fly by the server as your site runs and is compiled (and so don't exist in your project), while in a web application they are created and managed by Visual Studio - if they don't exist, then the code will potentially fail to compile because the objects haven't been instatiated - what is the actual compiler error are you seeing?
By adding a new control to the page after you've converted the project to a web application, you are forcing VS to create the partial class that was missing.

Resources