将数据绑定到DetailView中的标签

时间:2014-06-06 14:47:52

标签: c# asp.net sql detailsview

我在数据库中有两个表。

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

我想要做的是,在详情视图中显示表格'电子邮件'而不是显示' 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

详情视图如下:

type       1
mxRecord   NULL
clientId   1

我希望它看起来像这样..

type       Exchange
mxRecord   NULL
clientId   1

1 个答案:

答案 0 :(得分:0)

假设您正在使用带有上面查询的SqlDataSource,这只是在DetailsView中为typeName列创建一个boundfield条目。

切换到您的“源”视图,您应该可以轻松地将其显示为:

   <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>