使用从文本框输入的值绑定Gridview [英] Bind Gridview with value entered from textbox

查看:55
本文介绍了使用从文本框输入的值绑定Gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Gridview绑定了一个下拉列表它工作正常,我如何在textbox中使用相同的概念。下拉列表的代码是:

I have binded a dropdownlist with Gridview it works fine, how do i use the same concept in textbox.Code for Dropdownlist is:

<div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:TextBox ID="txtsearchbox" runat="server" 

            ontextchanged="txtsearchbox_TextChanged"></asp:TextBox>
        <ajax:AutoCompleteExtender ID="AutoCompleteExtender1"  runat="server" TargetControlID="txtsearchbox" ServiceMethod="SearchCustomers" MinimumPrefixLength="1">
        </ajax:AutoCompleteExtender>
      
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>




protected void Page_Load(object sender, EventArgs e)
   {
       if(!IsPostBack)
       {
           string cn = ConfigurationManager.ConnectionStrings["testing"].ConnectionString;
           using(SqlConnection connection =new SqlConnection(cn))
           {
               SqlCommand command = new SqlCommand("select * from Country",connection);
               connection.Open();
               SqlDataReader dr = command.ExecuteReader();
               ddllist.DataSource = dr;

               ddllist.DataBind();
               ddllist.Items.Insert(0, new ListItem("Select","0"));
               GetData();
           }


       }


   }
   private void GetData()
   {
       string cn = ConfigurationManager.ConnectionStrings["testing"].ConnectionString;
       using (SqlConnection connection = new SqlConnection(cn))
       {
           SqlCommand command = new SqlCommand("select * from Country where Id=@Id", connection);
           command.Parameters.AddWithValue("@Id", ddllist.SelectedValue);
           connection.Open();

           SqlDataReader drr = command.ExecuteReader();
           GridView1.DataSource = drr;
           GridView1.DataBind();
       }

   }
   protected void ddllist_SelectedIndexChanged(object sender, EventArgs e)
   {
       GetData();
   }





对于文本框假设(就像现在一样)该值存在于数据库中但是它不起作用





For textbox assuming(as if now) that value is there in database but its not working

protected void txtsearchbox_TextChanged(object sender, EventArgs e)
   {
       string cn = ConfigurationManager.ConnectionStrings["testing"].ConnectionString;
       using (SqlConnection connection = new SqlConnection(cn))
       {
           //Select * from mytable where colName LIKE '%' + mytextbox.Text + '%'
           //SqlCommand command = new SqlCommand("select * from country where EmployeeName=@EmployeeName", connection);
           //command.Parameters.AddWithValue("@EmployeeName",str);
           //SqlCommand command = new SqlCommand("select * from country where EmployeeName like '%' +txtSearch.Text+ '%'", connection);
           SqlCommand command = new SqlCommand("select * from country where EmployeeName=@EmployeeName", connection);
           command.Parameters.AddWithValue("@EmployeeName", txtsearchbox.Text.Trim());
           //command.Parameters.AddWithValue("@EmployeeName",str);
           connection.Open();
           SqlDataAdapter da = new SqlDataAdapter(command);
           DataSet ds = new DataSet();
           da.Fill(ds);
           //SqlDataReader dr = command.ExecuteReader();

           GridView1.DataSource = ds;
           GridView1.DataBind();
       }
   }

推荐答案

hi,



哦okok ..





在文本框代码中设置Autopostback属性为true AutoPostBack =true 然后再试一次..







oh okok..


Set Autopostback property is true in textbox code AutoPostBack="true" and try again..


<asp:TextBox ID="txtsearchbox" runat="server"

            ontextchanged="txtsearchbox_TextChanged" AutoPostBack="true" ></asp:TextBox>


这篇关于使用从文本框输入的值绑定Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆