【问题标题】:Change Display of Column Name in DetailsView更改详细信息视图中列名的显示
【发布时间】:2013-06-09 04:49:34
【问题描述】:

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

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

【问题讨论】:

    标签: c# database ado.net


    【解决方案1】:

    设置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 页面标记中有很多地方需要修复,我更改了一些但它不起作用,您的更新命令中没有 where 条件。如果您进行编辑,那么数据库中的所有记录都会更新。

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

    【讨论】:

    • 谢谢!我还需要做些什么才能将其显示在页面上吗?我只看到该表的编辑链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多