开发者

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 whi开发者_StackOverflow中文版ch 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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜