简体   繁体   中英

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

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.

That's the approach you have to use, but there is no way to get a complete list of controls, especially when you factor in potential third party controls. Basically, add to that list the other controls you are considering... The two interfaces above are pretty good ones to eliminate a range of controls...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM