【问题标题】:Clear Textboxes清除文本框
【发布时间】:2012-06-19 17:52:46
【问题描述】:

大家早安,

我想在我的网页上有一个取消按钮,基本上我想清除表单字段并将用户重定向到主页。

在页面重定向之前,我需要清除 7 个 txt 框。我在互联网上进行了一些搜索,并试图将以下示例放入我的页面中,但没有成功....

使用此代码,我在 X = "" 行上出现错误。我收到一条消息“字符串类型的值无法转换为 system.we.UI.control”

 Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

         Dim x As Control
           For Each x In Me.Controls
         If TypeOf x Is TextBox Then
           x = " "
         End If

         Next

 End Sub

我也尝试了以下也产生编译错误的方法。

        For Each c As Control In Me.Controls
        If TypeOf c Is TextBox Then
            DirectCast(c, TextBox).Text = ""
        End If
        Next

谁能帮我解决这个问题?

问候 贝蒂

【问题讨论】:

    标签: asp.net visual-studio-2010 asp.net-controls


    【解决方案1】:

    试试这个:

    Dim x As Control
    For Each x In Me.Controls
      If TypeOf x Is TextBox Then
        Dim txt as TextBox = x
        txt.Text = ""
      End If
    Next
    

    说明: 您尝试将字符串设置为Control-variable,当然编译器不知道如何做到这一点。 我给出的版本会将每个 TextBox 的 Text-property 设置为空字符串

    【讨论】:

      【解决方案2】:

      您可以使用 html 代码重置您正在使用的当前表单的所有字段

      使用以下代码重置所有字段

      <input type="reset" value="Clear" onclick="redirectFunction()" />
      

      并在 redirectFunction 中编写以下 javascript 代码:

      function redirectFunction()
      {
      window.location="destination.aspx";
      }
      

      使用上面的代码,您可以重定向到目标页面。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-06
        • 2012-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多