在DetailView中将数据绑定到标签

本文关键字:标签 数据绑定 DetailView | 更新日期: 2023-09-27 18:07:00

我在数据库中有两个表。

Tables:
       email
            -typeId     **Foreign Key**
            -mxRecord
            -clientId
       emailType
            -typeId     **Primary Key**
            -typeName

我要做的是,在一个Detailview中显示表格'email'而不是显示'typeId'的数字,我希望它显示值'typeName'

The contents of the tables:
email
        typeId      mxRecord     clientId
           1          NULL           1
           3          NULL           2
           2          NULL           3
emailType
        typeId      typeName
           1        Exchange
           2          POP3
           3         Hosted

我添加了一个下拉列表来选择适当的客户机。(@i1是下拉引用)

SQL:
     SELECT emailType.*, email.* FROM email 
     INNER JOIN emailType ON email.typeId = emailType.typeId 
     WHERE email.clientId = @i1

detail>> detail>>

type       1
mxRecord   NULL
clientId   1

我想让它看起来像这样…

type       Exchange
mxRecord   NULL
clientId   1

在DetailView中将数据绑定到标签

假设您正在使用SqlDataSource和上面包含的查询,那么只需在DetailsView中为typeName列创建一个边界字段条目。

切换到Source视图,你应该可以很容易地使它看起来像这样:

   <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
        AutoGenerateRows="False" DataKeyNames="typeId, clientId" 
        DataSourceID="SqlDataSource1">
        <Fields>
            <asp:BoundField DataField="typeName" HeaderText="type" 
                InsertVisible="False" ReadOnly="True" SortExpression="typeName" />
            <asp:BoundField DataField="mxRecord" HeaderText="mxRecord" 
                InsertVisible="False" ReadOnly="True" SortExpression="mxRecord" />
            <asp:BoundField DataField="clientId" HeaderText="clientId" 
                InsertVisible="False" ReadOnly="True" SortExpression="clientId" />
        </Fields>
    </asp:DetailsView>