Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if the control or control interface is editable by the user?

I'm looping through an array of controls and need to know which controls an end-user has the ability to (via JavaScript or directly) change the value that gets posted back. Where can I find such a list?

So far I have this:

Private Function IsEditableControl(ByVal control As Control) As Boolean
    Return TypeOf control Is IEditableTextControl _
     OrElse TypeOf control Is ICheckBoxControl _
     OrElse GetType(ListControl).IsAssignableFrom(control.GetType()) _
     OrElse GetType(HiddenField).IsAssignableFrom(control.GetType())
End Function
like image 512
adam0101 Avatar asked Sep 13 '25 05:09

adam0101


1 Answers

I'm rather sure that you only need to know if that control implements IPostBackDataHandler.

Public Shared Function IsControlEditable(ByVal ctrl As Control) As Boolean
    Return TypeOf ctrl Is IPostBackDataHandler
End Function

"If you want a server control you design to examine form data that is posted back to the server by the client, you must implement the IPostBackDataHandler interface. The contract that this interface defines allows a server control to determine whether its state should be altered as a result of the post back, and to raise the appropriate events."

These are the classes that implement it:

  • CheckBox
  • CheckBoxList
  • DropDownList
  • HtmlInputCheckBox
  • HtmlInputFile
  • HtmlInputHidden
  • HtmlInputImage
  • HtmlInputRadioButton
  • HtmlInputText
  • HtmlSelect
  • HtmlTextArea
  • ImageButton
  • ListBox
  • RadioButtonList
  • TextBox

The big advantage of checking for implementing IPostBackDataHandler is that your function works also in future(with controls that will be added to the framework) and for third party controls.

like image 121
Tim Schmelter Avatar answered Sep 14 '25 20:09

Tim Schmelter