无法将System.Web.UI.HtmlControls.HtmlInputText强制转换为System.Web.UI.HtmlControls.HtmlGenericControl

时间:2019-03-20 09:41:58

标签: c# asp.net html5 htmlgenericcontrol

我正在尝试将htmlinputtext转换为htmlgenericcontrol

file.aspx:

<form runat="server" class="probootstrap-form">         
  <div class="form-group" style="margin-top: 20px">
    <label for="name">Markt Name:</label>
    <div class="form-field">
      <input type="text" id="name" runat="server" required="required" class="form-control" />
    </div>
  </div>
</form>

file.aspx.cs:

HtmlGenericControl name = (HtmlGenericControl)Form.FindControl("name");

错误:System.InvalidCastException:无法将类型为“ System.Web.UI.HtmlControls.HtmlInputText”的对象转换为“ System.Web.UI.HtmlControls.HtmlGenericControl”

1 个答案:

答案 0 :(得分:2)

documentation中,您可以看到HtmlInputText不继承自HtmlGenericControl,因此无法转换为HtmlGenericControl

HtmlInputText可以转换为HtmlInputControl是最通用的HtmlControlControlobjectControl在ASPNET编程中很有意义。