ASP .NET C# Lto ink ListItems in DropDownList to row items i



我正在使用GridViewSqlDataSource,但我想在下拉列表中显示不同的名称,但将其与数据库相关联。例如,我希望下拉列表显示"所有商店",但在数据库中它存储为"ALL"。这可能吗?谢谢:)

<asp:TemplateField HeaderText="Business Unit" SortExpression="BUSN_UNIT_CD">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList5" runat="server" 
                    DataSourceID="SqlDataSource6" DataTextField="BUSN_UNIT_DS" 
                    DataValueField="BUSN_UNIT_DS">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource6" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:jobCodeConnectionString %>" 
                    SelectCommand="SELECT DISTINCT [BUSN_UNIT_DS] FROM [BUSN_UNIT]">
                </asp:SqlDataSource>
            </ItemTemplate>
        </asp:TemplateField>

DropDownList 存储在 GridView 中。

我想你可以使用不同的值和不同的文本,将值传递给数据库(全部)。

       <asp:DropDownList ID="ddlGender" runat="server" Width="200px">
            <asp:ListItem Text="All Stores" Value="ALL"></asp:ListItem>
            <asp:ListItem Text="ONE Store" Value="ONE"></asp:ListItem>
            <asp:ListItem Text="Two Store" Value="TWO"></asp:ListItem>
        </asp:DropDownList>

"文本"将在 UI 中看到,但值可以传递给数据源或数据库。

使用这个:

首先将记录从数据库插入网格视图,然后将数据加载到下拉列表中,如下所示;

DataTable dt = querytodbHere_Return_DataTable; 
 DropDownList lst = new DropDownList();
 foreach (DataRow drow in dt.Rows)
   {
    lst = (DGGeneric.Rows[cnt].Cells[0].FindControl("drpaction") as DropDownList);
    lst.Items.Add(new ListItem("--Select Action--"));
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
     lst.Items.Add(new ListItem(dr[1].ToString()));                              
    }    
     cnt += 1;
  }

我在这里尝试使用 linq 表达式,我将所有第一个固定记录与数据库所有记录联合起来。

.ASPX:

<asp:DropDownList ID="dllist" runat="server">
</asp:DropDownList>

.CS:

DataTable dt = your_data_source;
var i = (from j in new string[] { "" }
                 select new
                 {
                     TEXT = "All Stores",
                     VALUE = "ALL",
                 }).AsEnumerable().Union(from k in dt.AsEnumerable()
         select new { TEXT = k.Field<string>("TEXT"), VALUE = k.Field<string>("VALUE") });
    dllist.DataSource = i;
    dllist.DataValueField = "VALUE";
    dllist.DataTextField = "TEXT";
    dllist.DataBind();

我希望这可能会有所帮助。

因此,

您可以尝试以下建议:

方法一:在查询中操作数据库值,如下所示:

 Select DISTINCT CASE [BUSN_UNIT_DS] WHEN 'ALL' THEN 'ALL STORE' ELSE [BUSN_UNIT_DS] END FROM [BUSN_UNIT]

方法2:使用 DataBound 方法更改 DropDownList 的值:

<asp:DropDownList ID="DropDownList5" runat="server" 
  DataSourceID="SqlDataSource6" DataTextField="BUSN_UNIT_DS" 
  DataValueField="BUSN_UNIT_DS" AppendDataBoundItems="True"
  OnDataBound="DropDownList5_DataBound">
</asp:DropDownList>

代码隐藏:

protected void DropDownList5_DataBound(object sender, EventArgs e)
{
    DropDownList ddltype = (DropDownList)sender;
    foreach (ListItem item in ddltype .Items)
    {
        if (item.Text == "ALL")
        {
            item.Text = "ALL STORE";
        }
    }
}

最新更新