0

I have a textbox that I would to disable so users can't enter in any text they want. To choose a driver they must click on the magnify glass image and a list of drivers appears.

enter image description here

When they select a driver from the list, it populates the driver into the textbox. But when I go to save it, it value comes in as blank so no driver gets saved. The problem is setting the textbox to Enabled = false.

txtDriverName.Enabled = false;

If I remove this line the driver will save. But disabling the textbox saves a blank value. How do I stop users entering text in the textbox but still save the value that gets populated from the list?

user123456789
  • 1,914
  • 7
  • 44
  • 100
  • 1
    Related or duplicate: http://stackoverflow.com/questions/6316399/disabled-textbox-losses-viewstate But how and when do you assign a value to the `TextBox`? Try to do that on aspx or in `Page_Init`. – Tim Schmelter Jan 28 '16 at 14:18
  • @TimSchmelter I have a function that populates all the textboxes. This function is called from the `Page_Load` method. But thanks the question you linked worked – user123456789 Jan 28 '16 at 14:23
  • Try to call that method from `Page_Init` instead, interested in the result. – Tim Schmelter Jan 28 '16 at 14:38

2 Answers2

0

Try using:

txtDriverName.ReadOnly = true;
Rob W.
  • 270
  • 6
  • 14
0

Answer is setting the textbox to txtDriverName.Attributes.Add("readonly", "readonly");.

Not sure why this worked and setting it to Enabled = false or txtDriverName.ReadOnly = true; didn't work. But it saves the value in the textbox now

user123456789
  • 1,914
  • 7
  • 44
  • 100
  • 1
    Because this ensures that the `ViewState` is used to persist the value. If you set `Enabled=False` on serverside ASP.NET will disable viewstate for this control since a disabled control is not meant to change it's value across postbacks. – Tim Schmelter Jan 28 '16 at 14:39