我想实现一个查询功能。用了一个下拉列表dropdownlist控件,id是list,里面有三个选项:标题(title)、内容(content)、作者(author)。在写if条件语句时遇到了一个小问题,请高手指教。如下: 
string sql="select title,content,author from info where id='"+id+"' ; 
if(选择的是内容) //这里的条件怎么写?

sql=sql+" and content='"+content+"'; 

if(选择的是标题) //这里的条件怎么写?

sql=sql+" and title='"+title+"'; 

解决方案 »

  1.   

    可能我的描述有点不清楚,我把页面代码贴出来吧:
    <INPUT class=inputbg name=keyword id="keyword" runat="server"> <BR><asp:DropDownList id="where" runat="server" AutoPostBack="True" onselectedindexchanged="c_SelectedIndexChanged">
        <asp:ListItem Value="title">标题</asp:ListItem>
        <asp:ListItem Value="writer">作者</asp:ListItem>
        <asp:ListItem Value="content">内容</asp:ListItem>
    </asp:DropDownList> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="搜索" />
     
    在后台怎么实现查询功能?谢谢!!
      

  2.   

     string sql="select title,content,author from info where id='"+id+"' ;  protected void Button1_Click(object sender, EventArgs e)
     {
            sql += " and " + this.where.SelectedValue + " like('%" + this.keyword.Value + "%')";
            Response.Write(sql);
     }
      

  3.   

    if(DropDownList.SelectedItem.Value=="content")//内容
    {}
      

  4.   


    if(list.SelectedValue=="title")
    {
    //标题查询
    }
    if(list.SelectedValue=="writer")
    {
    //作者查询}
    if(list.SelectedValue=="content")
    {
    //内容查询}
      

  5.   


    if(list.SelectedValue=="title")
    -->这个是拿值,如果你要拿下拉列表的文本应该是list.SelectedItem.Text 都可以用。看你的需求
    {
    //标题查询
    }
    if(list.SelectedValue=="writer")
    {
    //作者查询}
    if(list.SelectedValue=="content")
    {
    //内容查询}
      

  6.   


    http://topic.csdn.net/u/20080823/15/8c8811ca-03e6-4049-a99a-c0ca380bde9e.html5楼有很好的办法