2012-09-07 139 views
0

我将如何实现这个:搜索框,按钮和GridView

用户可以在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) 
     { 

     } 
    } 
} 

回答

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文本框,就像

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

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

+0

感谢清楚......但我有麻烦键入搜索和GridView中返回的搜索结果仍然 – user1179083

+0

您可以发布更完整的代码? – Roland

+0

想通了,谢谢,我必须使用表适配器功能等。 – user1179083