I have a GridView that has records in it. When you select one of the records it updates the detailsview so you can makes changes. I have them set as templates. One of the fields that I am pulling from SQL is a number 1-4 (A priority level). I want it so that when you select the record in the gridview, it shows the data in the Detailsview. Now, as of right now, it showing the number. But I wrote another method to convert the number into text (switch statement) to show what the number mean. This page is written as a ASCX files, just fyi. I need to know what code that I need to on the back end to make it so that my method can switch the number for Label6 it the text.
I omitted some code to save time.
<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>
You can call methods in your code behind from the aspx page easy enough.
Code Behind with method to convert priority to a string (I have assumed it is an int)
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";
}
}
}
Then in the aspx just call this method passing in your value
<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>
Seeing as you now have the list of priorities twice (once in the aspx and once in the code behind) you would probably be better off making it a list and binding the drop down list to that as well as reading the name from it in-case you decide to change a name / add another priority it only needs to be done in one spot.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.