Why can't I get the value when I disable a control?

M

Mike Hnatt

I have code that will set a drop-down list value to "xyz" and the .Enabled
property to False. However, when I do that I can no longer retrieve the
value "xyz" upon postback. It is as if I set the .Visible property to False
as well and the control wasn't rendered, but I did not. If you View Source
of the rendered HTML, you can see that the control is indeed still intact
(everything is the same except the disabled argument is set to "disabled").

Why can't I get the value when I disable a control?

Thanks,
Mike
 
L

L. L.

Hi Wim,

Is it by design for version 1.1? I am using 1.0 and this is not happening.


Thanks,

Lenny

Wim Hollebrandse said:
That is by design.

What you can do however is to set a hidden form field to the value of the
selectbox just before you disable it, and retrieve that instead.
 
M

Mike Hnatt

Thanks Wim,
Good idea,
Mike

Wim Hollebrandse said:
That is by design.

What you can do however is to set a hidden form field to the value of the
selectbox just before you disable it, and retrieve that instead.
 
D

David

Is it by design for version 1.1? I am using 1.0 and this is not happening.

It's by design in the HTML spec. ASP.NET versions have nothing to do
with it. Browsers aren't supposed to submit the values of disabled
controls.
 
L

L. L.

Mike,

For a dropdown, you don't set the value like
DropDownList1.SelectedItem.Value = "xyz". Instead, you do this
DropDownList1.SelecteIndex = i; //i is the index of value "xyz" in the
dropdown, starting from 0.

Make sure only set it once in the Page_Load event like,

if(!Page.IsPostBack)

{

DropDownList1.SelectedIndex = 1;

DropDownList1.Enabled = false;//you can still get the value on postaback

DropDownList1.Visible = false;//you can still get the value on postaback

}

In the postback, I am sure you can get the value through code like,

string ddlvalue = DropDownList1.SelectedItem.Value;

It doesn't matter the ddl is enabled or not. The ddl can even be invisible.

L.L.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top