0

我的 FormView(学生)有一个 ObjectDataSource,FormView 中的 DropDownList(名称)有另一个 ObjectDatSource。如果 DropDownList 的源不包含与 formview 的源匹配的名称值,我想显示“不可用”。目前我有这个代码,如果数据源返回一个 NULL 值,它就可以工作。当 FormView 名称值不在 DropDownList 的数据绑定列表中时,如何将其更改为显示“不可用”?

<asp:FormView ID="Students" runat="server" DataSourceID="Students_DataSource">
    <ItemTemplate>
        <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
    </ItemTemplate>
    <EditTemplate>
        <asp:DropDownList ID="ddlName" runat="server" 
        SelectedValue='<%# If(Eval("Name") IsNot Nothing, Eval("Name"), "Not Available") %>' 
                    DataSourceID="fvAllNames" runat="server" DataTextField="Name" DataValueField="Name" />        
    </EditTemplate>
</asp:FormView>
4

1 回答 1

0
public class MyItem
{
    public string Name { get; set; }
    public int UserId { get; set; }
}

public void MethodThatLoadsMyData
{
    var originalListData = MethodThatFetchesMyData(45);
    myDropDownList.DataSource = MethodThatBumpsTwoItemDatasources(myOuterList, originalListData);
    myDropDownList.DataBind();
}

public void MethodThatBumpsTwoItemDatasources(List<MyItem> outerList, List<MyItem> dropdownList)
{
    /*This modifies the original result set from the database that you were going
    to use to populate your dropdown list. It compares the outer list (whatever that is)
    with the result set, and adds items called "Not Available" when there is an item in the
    outer list that doesn't exist in the dropdownlist */

    var result = new List<Item>();
    foreach (var item in listA)
    {
        var matched = false;
        foreach (var itemB in listB.Where(itemB => itemB.Id == item.Id))
        {
            matched = true;
            result.Add(item);
        }
        if (!matched)
        {
            result.Add(new Item
            {
                Name = "Not Available",
                Id = 0
            });
        }
        matched = false; 
    }
    return result;
}

public List<MyItem> MethodThatFetchesMyData(int myParameter)
{
    //gets my data from the database and builds dto objects
    var sql = "my_stored_procedure_or_sql";
    var list = new List<MyItems>();
    using(var conn = new SqlConnection(myConnectionString))
    {
        using(var comm = new SqlCommand(sql, conn))
        {
            //do your normal client setup here (sql type, parameters, etc//
            var parameters = SqlParameter[1];
            parameters[0] = new SqlParameter("@ParameterName", DbType.Int);
            parameters[0].Value = myParameter;
            comm.Parameters = parameters;
            comm.CommandType = CommandType.StoredProcedure;
            conn.Open();
            using(var rdr = comm.ExecuteReader())
            {
                while(rdr.Read())
                {
                    list.Add(
                        new MyItem{
                            Name = rdr["NameColumn"].ToString(),
                            UserId = rdr["ID"]
                        });
                }
                return list;
            }
        }
    }
}

In your databound controls, you can get at the new values in the itemtemplate with your typical

<%#Eval("Name") %> <%#Eval("UserId") %>

What I'm actually doing here is binding the controls to a list of actual objects instead of the datatable that is constructed with the datasource control. By doing that, I can do whatever I need to do with those lists before binding it to the control. In this case, I bump the two lists together and add items for those that do not exist in one list but exist in the other. Not sure if this is exactly what you needed, but this should be enough to give you some ideas.

于 2012-10-12T17:14:33.877 回答