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

  • Thread starter Thread starter Mike Hnatt
  • Start date Start date
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
 
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.
 
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.
 
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.
 
Back
Top