簡體   English   中英

DetailsView在選擇時更新ItemTemplate

[英]DetailsView Update ItemTemplate on Select

我有一個其中包含記錄的GridView。 當您選擇一條記錄時,它會更新detailsview,以便您進行更改。 我將它們設置為模板。 我從SQL中提取的字段之一是1-4(優先級)。 我想要它,以便當您在gridview中選擇記錄時,它在Detailsview中顯示數據。 現在,截至目前,它顯示的是數字。 但是我寫了另一種方法將數字轉換成文本(switch語句)以顯示數字的含義。 此頁面被編寫為ASCX文件,僅供參考。 我需要知道需要在后端執行哪些代碼,以便我的方法可以為文本切換Label6的編號。

我省略了一些代碼以節省時間。

    <asp:DetailsView ID="dvRequestDetails" runat="server" AutoGenerateRows="False" CellPadding="4"
                            DataKeyNames="casenumber" DataSourceID="RequestDetails" Font-Names="Arial" Font-Size="Small"
                            ForeColor="#333333" GridLines="None" Height="50px" Width="300px" 
                            EnableModelValidation="True" 
                            onpageindexchanging="dvRequestDetails_PageIndexChanging">
                            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
                            <EditRowStyle BackColor="WhiteSmoke" />
                            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" VerticalAlign="Top" />
                            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            <Fields>    
    <asp:TemplateField HeaderText="Priority" SortExpression="other">
                                    <EditItemTemplate>
                                        <asp:DropDownList ID="ddlDetailsOther" runat="server" AutoPostBack="True" 
                                            SelectedValue='<%# Bind("other") %>' OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                                            <asp:ListItem Value="4" Selected="True">General</asp:ListItem>
                                            <asp:ListItem Value="3">Low Priority</asp:ListItem>
                                            <asp:ListItem Value="2">High Priority</asp:ListItem>
                                            <asp:ListItem Value="1">Critical</asp:ListItem>
                                        </asp:DropDownList>
                                    </EditItemTemplate>
                                    <InsertItemTemplate>
                                        <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("other") %>'></asp:TextBox>
                                    </InsertItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("other") %>' OnDataBound="dvRequestDetails_DataBound"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
</Fields>
                        <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" VerticalAlign="Top" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    </asp:DetailsView>

您可以在aspx頁面后面的代碼中輕松調用方法。

代碼背后的方法將優先級轉換為字符串(我假設它是一個整數)

public partial class DemoPage : System.Web.UI.Page
{
    protected string GetPriorityName(object priority)
    {
        switch ((int)priority)
        {
            case 1:
                return "Critical";
            case 2:
                return "High Priority";
            case 3:
                return "Low Priority";
            case 4:
                return "General";
            default:
                return "Unknown";
        }
    }
}

然后在aspx中只需調用此方法即可傳遞您的值

<asp:DetailsView ID="dvRequestDetails" runat="server">
    <Fields>    
        <asp:TemplateField HeaderText="Priority" SortExpression="other">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlDetailsOther" runat="server" AutoPostBack="True" 
                    SelectedValue='<%# Bind("other") %>' OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Value="4" Selected="True">General</asp:ListItem>
                    <asp:ListItem Value="3">Low Priority</asp:ListItem>
                    <asp:ListItem Value="2">High Priority</asp:ListItem>
                    <asp:ListItem Value="1">Critical</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox6" runat="server" Text='<%# GetPriorityName(Bind("other")) %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label6" runat="server" Text='<%# GetPriorityName(Bind("other")) %>' OnDataBound="dvRequestDetails_DataBound"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>

既然您現在有了兩次優先級列表(一次在aspx中,一次在后面的代碼中),則最好將其設置為列表並將下拉列表綁定到該列表,並在其中讀取名稱。如果您決定更改名稱/添加其他優先級,則只需在一個地方進行即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM