更改DetailsView中列名的显示

本文关键字:显示 DetailsView 更改 | 更新日期: 2023-09-27 18:17:38

我正在创建一个用户可以编辑其用户信息的页面。我在ASP中使用DetailsView控件来显示数据。如何更改字段的名称?"FirstName"显示为"FirstName"?

        <asp:SqlDataSource ID="sauceatronConnString" runat="server" 
        ConnectionString="<%$ ConnectionStrings:sauceatronConnString %>"
        ProviderName="<%$ ConnectionStrings:sauceatronConnString.providerName %>"
        SelectCommand="SELECT FirstName, LastName, ShipAddress, ShipCity, ShipState, ShipZipCode, ShipCountry, Phone, Email, Pwd, BillAddress, BillCity, BillState, BillZipCode, BillCountry FROM [Customers]" 
        UpdateCommand="UPDATE [Customers] SET [Firstname] = @Firstname, 
        [Lastname] = @Lastname, [Age] = @Age, 
        [IsFullTime] = @IsFullTime, [Username] = @Username, [Password] = @Password"></asp:SqlDataSource>

        <asp:DetailsView ID="DetailsView1" runat="server" DataSourceId="sauceatronConnString" AutoGenerateRows="true" AutoGenerateEditButton="True" />
        <Fields>
            <asp:BoundField DataField="FirstName" HeaderText="CustomerID" ReadOnly="True" />
            <asp:BoundField DataField="LastName" HeaderText="ContactName" />
            <asp:BoundField DataField="ShipAddress" HeaderText="ContactTitle" />
            <asp:BoundField DataField="ShipCity" HeaderText="CompanyName" />
            <asp:BoundField DataField="ShipZipCode" HeaderText="Address" />
            <asp:BoundField DataField="ShipCountry" HeaderText="City" />
            <asp:BoundField DataField="Phone" HeaderText="Region" />
            <asp:BoundField DataField="Email" HeaderText="PostalCode" />
            <asp:BoundField DataField="Pwd" HeaderText="Country" />
            <asp:BoundField DataField="BillAddress" HeaderText="Phone" />
            <asp:BoundField DataField="BillCity" HeaderText="Fax" />
            <asp:BoundField DataField="BillState" HeaderText="Phone" />
            <asp:BoundField DataField="BillZipCode" HeaderText="Fax" />
        </Fields>
        <UpdateParameters>
            <asp:Parameter Name="Firstname" Type="String"/>
            <asp:Parameter Name="Lastname" Type="String"/>
            <asp:Parameter Name="Age" Type="Int32"/>
            <asp:Parameter Name="IsFullTime" Type="Boolean"/>
            <asp:Parameter Name="Username" Type="String"/>
            <asp:Parameter Name="Password" Type="String"/>
        </UpdateParameters> 

        <asp:/DetailsView>

</asp:Content>

更改DetailsView中列名的显示

设置AutoGenerateRows="false",因为您已经定义了DetailsView中的所有字段

现在您可以为标题文本提供有意义的名称,如下面

设置标题文本:

<asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="ShipAddress" HeaderText="Ship Address" />

请参考一些关于ASP.NET的教程或书籍。在你的aspx页面标记中有很多地方需要修复,我已经改变了一些,但它不起作用,你没有更新命令中的条件。如果您编辑,那么数据库中的所有记录都将更新。

<asp:SqlDataSource ID="sauceatronConnString" runat="server" ConnectionString="<%$ ConnectionStrings:sauceatronConnString %>"
    ProviderName="<%$ ConnectionStrings:sauceatronConnString.providerName %>" SelectCommand="SELECT FirstName, LastName, ShipAddress, ShipCity, ShipState, ShipZipCode, ShipCountry, Phone, Email, Pwd, BillAddress, BillCity, BillState, BillZipCode, BillCountry FROM [Customers]"
    UpdateCommand="UPDATE [Customers] SET [Firstname] = @Firstname, 
[Lastname] = @Lastname, [Age] = @Age, 
[IsFullTime] = @IsFullTime, [Username] = @Username, [Password] = @Password">
    <UpdateParameters>
        <asp:Parameter Name="Firstname" Type="String" />
        <asp:Parameter Name="Lastname" Type="String" />
        <asp:Parameter Name="Age" Type="Int32" />
        <asp:Parameter Name="IsFullTime" Type="Boolean" />
        <asp:Parameter Name="Username" Type="String" />
        <asp:Parameter Name="Password" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="sauceatronConnString"
    AutoGenerateRows="false" AutoGenerateEditButton="True">
    <Fields>
        <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name" />
        <asp:BoundField DataField="ShipAddress" HeaderText="Ship Address" />
        <asp:BoundField DataField="ShipCity" HeaderText="Ship City" />
        <asp:BoundField DataField="ShipZipCode" HeaderText="Ship Zip Code" />
        <asp:BoundField DataField="ShipCountry" HeaderText="Ship Country" />
        <asp:BoundField DataField="Phone" HeaderText="Phone" />
        <asp:BoundField DataField="Email" HeaderText="Email" />
        <asp:BoundField DataField="Pwd" HeaderText="Pwd" />
        <asp:BoundField DataField="BillAddress" HeaderText="Bill Address" />
        <asp:BoundField DataField="BillCity" HeaderText="Bill City" />
        <asp:BoundField DataField="BillState" HeaderText="Bill State" />
        <asp:BoundField DataField="BillZipCode" HeaderText="Bill Zip Code" />
    </Fields>
</asp:DetailsView>