【问题标题】:search box, button and gridview搜索框、按钮和网格视图
【发布时间】:2012-09-07 15:20:56
【问题描述】:

我将如何实现:

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

         }
     }
}

【问题讨论】:

    标签: asp.net datagrid


    【解决方案1】:

    一个提示。不要这样做:

    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();
    

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

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

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

    【讨论】:

    • 感谢您的明确...但在输入搜索和在 gridview 中返回搜索结果时仍然遇到问题
    • 明白了,谢谢,我必须使用表格适配器功能等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多