首先,我在我的程序前台页面中的  DropDownList 空间是用
<asp:DropDownList ID="hp_grouInfo_gj_name" runat="server" RepeatLayout="Flow" RepeatColumns="5"
 Width="100%" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="gj_name" DataValueField="gj_name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ConnectionString="<%$ ConnectionStrings:Jcjg_JMConnectionString %>" 
  SelectCommand="SELECT [gj_name] FROM [gj_name]"></asp:SqlDataSource>这个方法绑定数据的,但我想在后台写个方法  可以控制 这个控件里的数据由  subject决定  注:{subject是数据库中 gj_name表中的一个 subject字段 , gj_name表中有3个字段 , 一个name_id   一个gj_name 一个subject }而我现在上面页面上的代码 是直接绑定gj_name表中的 gj_name字段 所以我的DropDownList 显示的时候就是所有的名称  所以我想用 gj_name中 subject 字段做判断的显示数据我的方法  :protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (Request["subject"] != null && Request["subject"].ToUpper() == "HP_GROU")
        {
            hp_grouInfo_gj_name.SelectedIndex = 0;
        }
        else
        {
            hp_grouInfo_gj_name.SelectedIndex = 1;
        }
    }我是菜鸟  如果说的不清楚很抱歉,希望高手能帮我解决  呵~

解决方案 »

  1.   

    页面都没有传值,后台用Request["subject"]有什么用?
      

  2.   

    Request["subject"]  这个我知道用错   现在我用这个方法只可以固定绑定  但是不能判断  袄哦``
      

  3.   

    想用subject做条件直接
    string subject=String.Empty;
    SqlCommand cmd=new SqlCommand(select *from subject where subject='"+subject+"',conn);用一个subject变量把你的查询条件带进去,每次查询出的结果绑定DropDownList不就好了?
      

  4.   

    我在说清楚点吧   我做一个 DropDownList 的下拉框    我要这个下拉框里面的数据可以根据一个表中的字段来判断显示或 不显示  显示就有数据 不显示就没数据我页面现在是这样写:
    <td colspan="2">
                   <asp:DropDownList ID="hp_grouInfo_gj_name" runat="server" RepeatLayout="Flow" RepeatColumns="5"
                        Width="100%" DataSourceID="SqlDataSource1" 
                        DataTextField="gj_name" DataValueField="gj_name">
                    </asp:DropDownList>
                   <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:Jcjg_JMConnectionString %>" 
                        SelectCommand="SELECT [gj_name] FROM [gj_name] where subject='hp_grou '"></asp:SqlDataSource> 
                </td>现在这个在页面上写的是直接棒定数据  可以判断显示或不显示  但是当我保存这个数据的时候  他在一次的显示为第一个值   肯定是因为我的SELECT [gj_name] FROM [gj_name] where subject='hp_grou ' 这个原因  但我后台的写不来  高手帮忙啊``注:{subject是数据库中 gj_name表中的一个 subject字段 , gj_name表中有3个字段 , 一个name_id  一个gj_name 一个subject } 而我现在上面页面上的代码 是直接绑定gj_name表中的 gj_name字段 所以我的DropDownList 显示的时候就是所有的名称  所以我想用 gj_name中 subject 字段做判断的显示数据 
      

  5.   

    想用subject做条件直接 
    string subject=String.Empty; 
    SqlCommand cmd=new SqlCommand(select *from subject where subject='"+subject+"',conn); 用一个subject变量把你的查询条件带进去,每次查询出的结果绑定DropDownList不就好了? 
      

  6.   


    <asp:DropDownList ID="hp_grouInfo_gj_name" runat="server" RepeatLayout="Flow" RepeatColumns="5" 
    Width="100%" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="gj_name" DataValueField="gj_name"> 
    </asp:DropDownList> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString=" <%$ ConnectionStrings:Jcjg_JMConnectionString %>" 
      SelectCommand="SELECT [gj_name] FROM [gj_name] WHERE ([subject] = @subject)"> 
      <SelectParameters>
      <asp:Parameter DefaultValue="自己填认值" Name="subject" Type="你subject字段的数据类型" />
      </SelectParameters>
    </asp:SqlDataSource> 后台你声明一个string类型的subject变量用来记录查询条件的String suject=String.Empty;
    this.SqlDataSource1.SelectParameters["subject"].DefaultValue =suject;//这个就是给参数的赋值语句
    this.SqlDataSource1.DataBind();
    再绑定你的DropDownList
      

  7.   

    所以我想用 gj_name中 subject 字段做判断的显示数据 这个你取出来的不只一个吧,subject 和gj_name要联系在一起的
      

  8.   

    因为你新加入的所以肯定是第一位了,根据你想要的值排序或者直接指定DropDownList的SelectIndex也行的
      

  9.   

     13L那DefaultValue是自己填默认值,空也行
    <asp:Parameter DefaultValue="默认值" Name="subject" Type="你subject字段的数据类型" />
      

  10.   

    17L  我在试你的方法    那我后台要写什么吗  这页面是这样的 我后台是这样的using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using HHHSoftware.Jcjg_JM.BLL;
    using System.Collections;
    using HHHSoftware.Jcjg_JM.Common;
    using HHHSoftware.Jcjg_JM.Model;public partial class UserControls_wtd_HP_Grou :UcBase
    {
         protected void Page_Load(object sender, EventArgs e)
         {
            if (!IsPostBack)
             {
                 if (hp_grouInfo_gj_name.Text == "")
                 {
                    // ControlHelper.BindDropDownListWithInfo<gj_nameInfo>("subject='hp_grou'", hp_grouInfo_gj_name, "gj_name", "gj_name", new List<HHH.Base.DBParameter>());
                 }
                 else
                 {
                     
                 }
                 
                //hp_grouInfo_gj_name.Items.Add();
             }     }
    }
      

  11.   

    你想在后台控制不会SqlDataSource那就用代码进行绑定呀
    try
            {
                string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connnane"];
                System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);
                //System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select [name_id],[gj_name] from [gj_name] where subject='" + Request["subjectid"].ToString + "'", conn);
                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select [name_id],[gj_name] from [gj_name] where subject='" + Request["subjectid"].ToString + "'", conn);            System.Data.DataSet ds = null;            da.Fill(ds);            this.DropDownList1.DataSource = ds.Tables[0];
                this.DropDownList1.DataTextField = "gj_name";
                this.DropDownList1.DataValueField = "name_id";
                this.DropDownList1.DataBind();        }
            catch (Exception ex)
            {        }
      

  12.   

    你定义变量名称都看不懂,这样很不好
    你后台只需给前台的subject传值啊:String suject=String.Empty;//保存你查询条件的变量
    this.SqlDataSource1.SelectParameters["subject"].DefaultValue =suject;//这个就是给subject传值
    this.SqlDataSource1.DataBind();
    DropDownList1.DataBind();
      

  13.   

    看了楼上这么多,我还是没搞清楚你的意图。
    通常我一般就是这样来做的 :
      先一条Sql语句   查出表 gj_name 中的所有数据
      然后 this.DropDownList.dataValueField=name_id  ;
          this.DropDownList.datatextField=gj_name ;
          this.DropDownList.dataBind();
           不过这样每次刷新页面也会同样绑定出数据库中所有的数据。
     哎 还是没懂你的意思,我也太菜了,领教了!
      

  14.   

    还有  将 查询出来的表  this.DropDownList.datasource= table1;