从tetxbox获取值,然后根据gridview中的下拉选择值显示数据 [英] Take Value from the tetxbox then based on dropdown selected value display data in gridview

查看:60
本文介绍了从tetxbox获取值,然后根据gridview中的下拉选择值显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和一个下拉列表,ddllist有两个值Id和Name,现在如果用户输入

1并在ddlist中选择Id,那么Id 1的数据应该显示在gridview中如果用户输入名称即。 myname并在ddlist中选择Name,然后显示基于myname的数据。



我已经通过两次按钮点击实现了这一点,但无法在ddl上实现此目的。

已实现代码



 <   table     cellpadding   =  0    cellspacing   =  0   < span class =code-attribute> class   =  style1 >  
< tr >
< td colspan = 2 >
< asp:TextBox ID = TextBox1 runat = 服务器 > < / asp:TextBox > < / td >
< td > < asp:ImageButton ID = ImageButton1 runat = server 高度 = 19px

ImageUrl = 〜/ images / calendar.png onclick = ImageButton1_Click 宽度 = 33px / > < / td >
< / tr >
< tr >
< td < span class =code-keyword>>
< asp:Button ID = ById runat = server Text = 按ID获取产品

onclick = Button3_Click / > < / td >
< td >
< asp:按钮 ID = ByName < span class =code-attribute> runat = server 文本 = 按名称获取产品

onclick = Button2_Click / > < / td >
< td >
< asp:按钮 ID = Button4 runat = server 文字 = 按日期获取产品

< span class =code-attribute> onclick = Button4_Click / > ; < / td >
< / tr > ;
< / t能够 >



按钮点击

 受保护  void  ById_Click( object  sender,EventArgs e)
{
string cn = ConfigurationManager.ConnectionStrings [ connectivity]。ConnectionString;
使用(SqlConnection con = new SqlConnection(cn))
{

SqlCommand command = new SqlCommand( spgetbyid,con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add( new SqlParameter( @id,TextBox1.Text));
con.Open();
GridView1.DataSource = command.ExecuteReader();
GridView1.DataBind();
}
受保护 void ByName_Click( object sender,EventArgs e)
{
string cn = ConfigurationManager.ConnectionStrings [ connectivity]。ConnectionString;
使用(SqlConnection con = new SqlConnection(cn))
{

SqlCommand command = new SqlCommand( spgetpro,con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add( new SqlParameter( @name,TextBox1.Text));
con.Open();
GridView1.DataSource = command.ExecuteReader();
GridView1.DataBind();
}
}



商店程序

 创建 precedure spgetbyid 
@ id int
< span class =code-keyword> as
begin
select * 来自 tblproducts 其中 id = @ id
结束

创建 precedure spgetpro
@ name varchar 10
as
开始
选择 * 来自 tblproducts 其中​​ name = @ name
end





希望在Dropdown中实现相同的功能

 <   asp:TextBox     ID   =  txttwo    runat   = 服务器 > ;  <   / asp:TextBox  > ;  
< asp:DropDownList ID = ddllist runat = server 高度 = 16px AutoPostBack = true
< span class =code-attribute> onselectedindexchanged = ddllist_SelectedIndexChanged >
< asp:ListItem = 0 > 选择< / asp:ListItem >
< < span class =code-leadattribute> asp:ListItem = 1 > Id < / asp:ListItem >
< asp:ListItem = 2 > 名称< / asp:ListItem >
< asp:ListItem = 3 > 日期< / asp:ListItem >
< / asp:DropDownList >





我知道如何根据Dropdown中的选定值在gridview中显示数据/>
例如

 受保护  void  Page_Load( object  sender,EventArgs e)
{
if ( !IsPostBack)
{
string cn = ConfigurationManager.ConnectionStrings [ 连接]的ConnectionString。
使用(SqlConnection connection = new SqlConnection(cn))
{
SqlCommand command = new SqlCommand( spgetcount,连接);
command.CommandType = CommandType.StoredProcedure;
connection.Open();
SqlDataReader dr = command.ExecuteReader();
ddl.DataTextField = country;
ddl.DataValueField = country;
ddl.DataSource = dr;
ddl.DataBind();
ddl.Items.Insert( 0 new ListItem( 请选择));
}


}

}
受保护 void ddl_SelectedIndexChanged( object sender,EventArgs e)
{
string cn = ConfigurationManager.ConnectionStrings [ connectivity]。ConnectionString;
使用(SqlConnection connection = new SqlConnection(cn))
{

SqlCommand command = new SqlCommand( select * from country,connection);
command.Parameters.AddWithValue( @ country,ddl.SelectedValue);
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
}

解决方案

您的存储过程应该是这样的

接受2个参数

1.标志 - 传递的价值类型

2.价值 - 通过什么价值



 创建 过程 dsp_GridData 

@ flag VARCHAR 10 ),
@ Value VARCHAR 10

BEGIN
SET NOCOUNT ON
IF @ flag = ' ID'
BEGIN
SELECT desired_field FROM YourTable 其中 id = CONVERT int ,< span class =code-sdkkeyword> @ value ) / * AS你的id是int * /
END

IF @ flag = ' Country'
BEGIN
SELECT desired_field FROM YourTable 其中 country = @ value / * AS你的id是int * /
END

AND 所以 ..........

SET NOCOUNT OFF
END





在您的C#代码

  protected   void  ddl_SelectedIndexChanged( object  sender,EventArgs e)
{
string flag;
if (ddlList.SelectedValue == ID
{
flag = ID;

}
依此类推 所有其他标志
string cn = ConfigurationManager.ConnectionStrings [ connectivity]。ConnectionString;
使用(SqlConnection connection = new SqlConnection(cn))
{

SqlCommand command = new SqlCommand( dsp_GridData,连接);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue( @ flag,flag);
command.Parameters.AddWithValue( @ value,TextBox1.Text);
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
}



请记住以下几点

1.切勿在任何存储过程中使用Select *

2.而不是创建4 - 5存储过程包括1 in


你可以使用sqldatasource来绑定gridview和参数的条件,它会如果你想做那么多话就很容易。



http://www.c-sharpcorner.com/uploadfile/raj1979/working-with-sqldatasource -control-in-Asp-Net-3-5 / [ ^ ]



使用ASP.NET SQLDATASOURCE [ ^ ]


替换为以下

ddl .selecteditem.value

I have a textbox and a dropdown list,the ddllist has two values Id and Name,now if the user enters
1 and selects Id in ddlist then data of Id 1 should be displayed in gridview and if a user enters Name ie. myname and selects Name in ddlist then data based on myname should be displayed.

I have achieved this on two button clicks but unable to achieve this on ddl.
Code for Achieved

<table cellpadding="0" cellspacing="0" class="style1">
        <tr>
            <td colspan="2">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
                <td><asp:ImageButton ID="ImageButton1" runat="server" Height="19px" 

        ImageUrl="~/images/calendar.png" onclick="ImageButton1_Click" Width="33px" /> </td>
        </tr>
        <tr>
            <td>
     <asp:Button ID="ById" runat="server" Text="Get Product by Id" 

        onclick="Button3_Click" /></td>
            <td>
             <asp:Button ID="ByName" runat="server" Text="Get Product by Name" 

        onclick="Button2_Click" /></td>
        <td> 
            <asp:Button ID="Button4" runat="server" Text="Get Product by Date" 

                onclick="Button4_Click" /></td>
        </tr>
    </table>


Button Click

protected void ById_Click(object sender, EventArgs e)
    {
        string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cn))
        {

            SqlCommand command = new SqlCommand("spgetbyid", con);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@id", TextBox1.Text));
            con.Open();
            GridView1.DataSource = command.ExecuteReader();
            GridView1.DataBind();
        }
protected void ByName_Click(object sender, EventArgs e)
    {
        string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cn))
        {

            SqlCommand command = new SqlCommand("spgetpro", con);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@name",TextBox1.Text));
            con.Open();
            GridView1.DataSource = command.ExecuteReader();
            GridView1.DataBind();
        }
    }


Store Procedure

create precedure spgetbyid
@id int
as
begin
select * from tblproducts where id=@id
end

create precedure spgetpro
@name varchar(10)
as
begin
select * from tblproducts where name=@name
end



Want to Achieve the same functionality in Dropdown

<asp:TextBox ID="txttwo" runat="server"></asp:TextBox>  
      <asp:DropDownList ID="ddllist" runat="server" Height="16px" AutoPostBack="true"
          onselectedindexchanged="ddllist_SelectedIndexChanged" >
          <asp:ListItem Value="0">Select</asp:ListItem>
          <asp:ListItem Value="1">Id</asp:ListItem>
          <asp:ListItem Value="2">Name</asp:ListItem>
          <asp:ListItem Value="3">Date</asp:ListItem>
      </asp:DropDownList>



I know how to Display data in gridview based on selected value in Dropdown
Eg

protected void Page_Load(object sender, EventArgs e)
  {
      if(!IsPostBack)
      {
          string cn=ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
          using(SqlConnection connection =new SqlConnection(cn))
          {
              SqlCommand command = new SqlCommand("spgetcount", connection);
              command.CommandType = CommandType.StoredProcedure;
              connection.Open();
              SqlDataReader dr = command.ExecuteReader();
              ddl.DataTextField = "country";
              ddl.DataValueField = "country";
              ddl.DataSource=dr;
              ddl.DataBind();
              ddl.Items.Insert(0,new ListItem("Please Select"));
          }


      }

  }
  protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
  {
      string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
      using (SqlConnection connection = new SqlConnection(cn))
      {

          SqlCommand command = new SqlCommand("select * from country", connection);
          command.Parameters.AddWithValue("@country", ddl.SelectedValue);
          SqlDataAdapter da = new SqlDataAdapter(command);
          DataSet ds = new DataSet();
          da.Fill(ds);
          GridView1.DataSource = ds;
          GridView1.DataBind();

      }
  }

解决方案

Your Stored Procedure should be like this
It will accept 2 Parameters
1. Flag - Which type of value passed
2. Value- What Value passed

Create Procedure dsp_GridData  
(
@flag VARCHAR(10),
@Value VARCHAR(10)
)
BEGIN
SET NOCOUNT ON
IF @flag = 'ID'
BEGIN
SELECT desired_field FROM YourTable where id = CONVERT(int,@value) /* AS your id is int*/
END

IF @flag = 'Country'
BEGIN
SELECT desired_field FROM YourTable where country = @value /* AS your id is int*/
END

AND so on..........

SET NOCOUNT OFF
END



In your C# code

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        string flag;
        if(ddlList.SelectedValue == "ID")
       {
         flag = "ID";
        
       }
       and so on for all other flag
        string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(cn))
        {
            
            SqlCommand command = new SqlCommand("dsp_GridData", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@flag", flag);
            command.Parameters.AddWithValue("@value", TextBox1.Text);
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
           
        }
    }


Remember following points
1. Never Use Select * in any stored procedures
2. Instead of creating 4 - 5 stored procedure include all in 1


you can use sqldatasource for the purpose of binding gridview with condition of parameter ,it would be easy if you want to do that much only.

http://www.c-sharpcorner.com/uploadfile/raj1979/working-with-sqldatasource-control-in-Asp-Net-3-5/[^]

Using the ASP.NET SQLDATASOURCE[^]


Replace with the following
ddl.selecteditem.value


这篇关于从tetxbox获取值,然后根据gridview中的下拉选择值显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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