将项目添加到 DataBound 下拉列表

问题描述 投票:0回答:7

是的,我已经阅读了这里的大部分主题,但我找不到有效的答案。

我有三个下拉列表。第一个是数据绑定以获取不同的实验名称。用户选择,页面回发,第二个下拉菜单显示不同的时间点。这就是我需要帮助的地方。我需要向该下拉列表中添加一个项目,其 ID、DataTextField、DataValueField 均为 TimePt。

看似简单,但我无法让它发挥作用。

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                TimePt.DataSource = TimePTDD;
                TimePt.DataValueField = "TimePt";
                TimePt.DataTextField = "TimePt";
                TimePt.DataBind();
                TimePt.Items.Insert(0, new ListItem("--Select---", "0"));
                TimePt.SelectedIndex = 0;
            }
        }

我错过了一些东西。

c# asp.net data-binding list drop-down-menu
7个回答
5
投票

在下拉列表中设置

AppendDataBoundItems="true"
,它应该可以工作。

这里有一个类似的问题:How to add Item to SqlDataSource databound list

还有一个关于使用此方法的潜在重复项及其解决方法:Dropdownlist AppendDataboundItems(第一个项目为空)


3
投票
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            TimePt.DataValueField = "TimePt";
            TimePt.DataTextField = "TimePt";
            var times = TimePTDD.ToList();
            times.Insert(0, new {TimePt="0",TimePt="--Select--"});
            TimePt.DataSource = times;
            TimePt.DataBind();
            //TimePt.SelectedIndex = 0;
        }
    }

0
投票
<asp:DropDownList ID="ExpAnalysisName" runat="server"
            DataSourceID="DropDownEXP" DataTextField="ExpAnalysisName" 
            DataValueField="ExpAnalysisName" AppendDataBoundItems="true" AutoPostBack=true>
            <asp:ListItem Selected="True" Value="0">Select an Experiment</asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="DropDownEXP" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SGMD3_DataBase %>" SelectCommand="SELECT DISTINCT ExpAnalysisName FROM VW_Data">
        </asp:SqlDataSource>

<asp:DropDownList ID="TimePt" runat="server" AutoPostBack="True" DataSourceID="TimePTDD" DataTextField="TimePt" 
                DataValueField="TimePt">
        </asp:DropDownList>
            <asp:SqlDataSource ID="TimePTDD" runat="server" 
                ConnectionString="<%$ ConnectionStrings:SGMD3_DataBase %>" SelectCommand="SELECT DISTINCT TimePt
    FROM VW_Data
    WHERE ExpAnalysisName = @ExpAnalysisName">
                <SelectParameters>
                    <asp:FormParameter FormField="ExpAnalysisName" Name="ExpAnalysisName" />
                </SelectParameters>
            </asp:SqlDataSource>

So you can have a reference.

0
投票

我看到您以两种不同的方式指定

DataSource
- 标记中的
DataSourceID
以及在代码隐藏中手动设置
DataSource
。尝试从标记中删除
DataSourceID
,看看是否有帮助。

自从我过多使用 ASP.NET 以来已经有一段时间了,但我有一种感觉,您的

DropDownList
Page_Load
之后重新绑定,这将取代您之前的绑定。


0
投票

我敢打赌这是一个页面生命周期问题。根据 MSDN,设置了 DataSourceID 属性的每个数据绑定控件都会调用其 DataBind 方法

我认为这些值与下拉列表绑定了两次。首先,当您在 Page_Load 中手动绑定并附加额外项目时,然后数据源将在 Page_PreRender 事件内绑定。 尝试将 Page_Load 代码引入 Page_PreRender。 希望订单有帮助。


0
投票

我建议使用

OnDataBound
控件的
DropDownList
事件,并将代码放在后面。 这样你就可以将
DataSourceID
与后面的代码结合起来。


0
投票

公共部分类Default12:System.Web.UI.Page { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        yourstate();
    }
}

public void yourstate()
{
    if (con.State == System.Data.ConnectionState.Closed)
    {
        con.Open();

    }
    SqlCommand cmd = new SqlCommand("ViewState", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    DropDownList1.DataSource = dt;
    DropDownList1.DataTextField = "mStateName";
    DropDownList1.DataValueField = "mStateId";
    DropDownList1.DataBind();
    DropDownList1.Items.Insert(0, new ListItem("select your state", "0"));
    con.Close();

}
public void yourdist(string stId)
{
    if (con.State == System.Data.ConnectionState.Closed)
    {
        con.Open();

    }
    SqlCommand cmd = new SqlCommand("ViewDistrict", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@DistrictStateId", stId);
    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    DropDownList2.DataSource = dt;
    DropDownList2.DataTextField = "mDistrictName";
    DropDownList2.DataValueField = "mDistrictId";
    DropDownList2.DataBind();
    DropDownList2.Items.Insert(0, new ListItem("select your state", "0"));
    con.Close();
}

public void yourcty(string ctyID)
{
    if (con.State == System.Data.ConnectionState.Closed)
    {
        con.Open();
    }
    SqlCommand cmd = new SqlCommand("ViewCity", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@CityDistrictId", ctyID);
    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    DropDownList3.DataSource = dt;
    DropDownList3.DataValueField = "mCityName";
    DropDownList3.DataValueField = "mCityId";
    DropDownList3.DataBind();
    DropDownList3.Items.Insert(0, new ListItem("select your cty", "0"));
    con.Close();

}
public void viewtype()
{
    if (con.State == System.Data.ConnectionState.Closed)
    {
        con.Open();

    }
    SqlCommand cmd = new SqlCommand("ViewType", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    DropDownList4.DataSource = dt;
    DropDownList4.DataTextField = "mTypeName";
    DropDownList4.DataValueField = "mTypeId";
    DropDownList4.DataBind();
    DropDownList4.Items.Insert(0, new ListItem(" your view", "0"));
    con.Close();
}

public void viewdetail(string viewID)
{
    if (con.State == System.Data.ConnectionState.Closed)
    {
        con.Open();

    }
    SqlCommand cmd = new SqlCommand("ViewTypeDetail", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@DetailTypeId", viewID);
    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    DropDownList5.DataSource = dt;
    DropDownList5.DataTextField = "mtDetailName";
    DropDownList5.DataValueField = "mtDetailId";
    DropDownList5.DataBind();

    DropDownList5.Items.Insert(0, new ListItem("your details", "0"));
    con.Close();
}



protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    yourdist(DropDownList1.SelectedValue);
}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    yourcty(DropDownList2.SelectedValue);
}

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{
    viewdetail(DropDownList4.SelectedValue);
}
© www.soinside.com 2019 - 2024. All rights reserved.