在webform中刷新下拉列表

时间:2013-08-12 07:59:11

标签: c#

我想在webform中刷新下拉列表。 我放了

<asp:DropDownList 
  ID="ddlNewJobOrder" 
  DataTextField="name" 
  DataValueField="value"
  runat="server" 
  AppendDataBoundItems="True" 
  DataSourceID="NewJobOrderDS" 
  ToolTip="New Job Order List" 
  Width="110" 
  AutoPostBack="True" 
  OnSelectedIndexChanged="ddlNewJobOrder_SelectedIndexChanged">
 </asp:DropDownList>

 ddlNewJobOrder.DataBind();

但它不起作用。

2 个答案:

答案 0 :(得分:2)

DataSourceID还不够。 它不是一个“必须”财产。您需要的属性是DataSource。 在服务器端,创建一个包含要添加的数据的对象并执行

ddlNewJobOrder.DataSource = dataObject;
ddlNewJobOrder.DataBind();

答案 1 :(得分:0)

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Sql_dalei" DataTextField="name1" DataValueField="flag1" AutoPostBack="True" OnSelectedIndexChanged="zhonglei_SelectedIndexChanged"></asp:DropDownList>

<asp:SqlDataSource ID="Sql_dalei" runat="server" 
ConnectionString="<%$ ConnectionStrings:SignBoardConnectionString%>"
SelectCommand="SELECT * FROM [TestCodeClass1]"></asp:SqlDataSource>

<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="Sql_zhonglei" DataTextField="name2" DataValueField="flag2" AutoPostBack="True" OnSelectedIndexChanged="nmnm_SelectedIndexChanged">
</asp:DropDownList>

<asp:SqlDataSource ID="Sql_zhonglei" runat="server"
 ConnectionString="<%$ ConnectionStrings:SignBoardConnectionString %>"
SelectCommand="SELECT * FROM [TestCodeClass2]"></asp:SqlDataSource>

----------------------------------------------------------------------------

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem { Text = "大類名稱", Value = "0" });
            DropDownList1.SelectedItem.Selected = true;
            tet.Attributes["class"] = "s2";
        }
    }

protected void zhonglei_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex != 0)
        {
            DropDownList2.Items.Clear();
            String s = DropDownList2.SelectedValue;
            Sql_zhonglei.SelectCommand = "SELECT * FROM [TestCodeClass2] where preflag1='" + DropDownList1.SelectedIndex + "'";
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, new ListItem { Text = "中類名稱", Value = "0" });
            tett.Attributes["class"] = "s2";
            tett2.Attributes["class"] = "s";
            if (DropDownList2.SelectedValue == "")
            {
                tett.Attributes["class"] = "s";


            }

        }
        else
        {
            tett.Attributes["class"] = "s";
            tett2.Attributes["class"] = "s";
        }

    }