locked
How to lookup a label for a value from a lookup table? RRS feed

  • Question

  • User260076833 posted

    Hello,

    I have a DataSet with a TableAdapter for rooms with the following attributes:

    idx int
    name varchar(50)
    type char(1)

    The column type references a table of types:

    symbol  name
    -----------------------
    K   kitchen
    C   conference
    O   office
    ...

    I have bound the DataSet to an ObjectDataSource which is used by a ListView.

    Within the ListView, I have the ItemTemplate:

    <ItemTemplate>
         <td align="left" style="" runat="server">
              <asp:Label ID="lbl_Index" runat="server" Text='<%# Eval("idx") %>' />
         </td>
         <td align="left" style="" runat="server">
              <asp:Label ID="lbl_Name" runat="server" Text='<%# Eval("name") %>' />
         </td>
         <td align="left" style="" runat="server">
              <asp:Label ID="lbl_Type" runat="server" Text='<%# Eval("type") %>' />
         </td>
    </ItemTemplate>
    

    However, the "type" is shown by its symbol, not by its name.
    I would like it to be shown by its name.

    I have tried to define the TableAdapter with a view instead of a table. But then there are no insert statements generated.

    What would be a simple way to lookup the name "on the fly" in the aspx-page?

    Thanks

    Yeoman

    Monday, August 3, 2015 10:31 AM

Answers

  • User61956409 posted

    Hi Yeoman,

    However, the "type" is shown by its symbol, not by its name.
    I would like it to be shown by its name.

    Firstly, you could select idx, name and typename from your two tables.

    select tb1.idx, tb1.name, tb2.name as 'typename' from table1 tb1 , table2 tb2 where tb1.[type]=tb2.symbol 

    Secondly, you could dynamically change the symbol  to typename in ListView ItemDataBound Event.

    <asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound">
        <LayoutTemplate>
            <table cellpadding="2" width="300px" border="1" runat="server" id="tblContacts">
                <tr id="itemPlaceholder" runat="server">
                </tr>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr runat="server">
                <td align="left" style="" runat="server">
                    <asp:Label ID="lbl_Index" runat="server" Text='<%# Eval("idx") %>' />
                </td>
                <td align="left" style="" runat="server">
                    <asp:Label ID="lbl_Name" runat="server" Text='<%# Eval("name") %>' />
                </td>
                <td align="left" style="" runat="server">
                    <asp:Label ID="lbl_Type" runat="server" Text='<%# Eval("type") %>' />
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("idx");
            dt.Columns.Add("name");
            dt.Columns.Add("type");
    
            dt.Rows.Add(1,"Name1","K");
            dt.Rows.Add(2, "Name2", "C");
            dt.Rows.Add(3, "Name3", "O");
    
            ListView1.DataSource = dt;
            ListView1.DataBind();
        }
    }
    
    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            string symbol = ((Label)e.Item.FindControl("lbl_Type")).Text;
            string type = "";
    
            switch (symbol)
            {
                case "K":
                    type = "kitchen";
                    break;
                case "C":
                    type = "conference";
                    break;
                case "O":
                    type = "office";
                    break;
    
                //...
                //other types
                //...
    
                default:
                    type = symbol;
                    break;
            }
            ((Label)e.Item.FindControl("lbl_Type")).Text = type;
        }
    } 
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 3, 2015 10:32 PM