搜索框,按钮和gridview

时间:2012-09-07 15:20:57

标签: asp.net datagrid

我将如何实现这一点:

用户可以在Textbox1中输入任何文字,然后点击Submitbtn1,然后在Gridview1中显示结果。 Clearbtn1用于清除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)
         {

         }
     }
}

1 个答案:

答案 0 :(得分: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放在Grid之外,比如

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

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

相关问题