0

我将如何实现这一点:

用户可以在 中输入任何文本Textbox1,然后单击Submitbtn1并在 中显示结果Gridview1Clearbtn1用于清除 Textbox1 中的所有文本。

这将在 Visual Studio ASP.NET Web 应用程序中完成。

这是我到目前为止所做的:

namespace SearchApplication
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void EmptyTextBoxValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if ((c.Controls.Count > 0))
                {
                     EmptyTextBoxValues(c);
                }
                else
                {
                     if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
                     {
                          ((TextBox)(c)).Text = "";
                     }
                     else
                     {
                         //do nothing
                     }
                 }
             }
         }

         protected void Button2_Click(object sender, EventArgs e)
         {
             EmptyTextBoxValues(this.Page);
         }

         protected void Button1_Click1(object sender, EventArgs e)
         {

         }
     }
}
4

1 回答 1

0

一个提示。不要做 :

if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
                     {
                          ((TextBox)(c)).Text = "";
                     }  .  

TextBox tx = c as TextBox. if(c != null) c.Text = "";  

至于其他的。单击提交按钮时,您将获取一些数据/创建一些数据并将其绑定到网格使用

grid.DataSource = myData;
grid.DataBind();

关于清除文本框。如果您使用放置在网格之外的普通 Asp.net TextBox,例如

<asp:TextBox id="Textbox1" runat="server"/>

您不需要 EmptyTextBoxValues。你可以做 Textbox1.Text = ""

于 2012-09-07T15:43:40.453 回答