有条件地显示服务器控件

时间:2015-11-10 20:13:46

标签: c# asp.net

我的任务是在我的小组项目中为所有用户创建“个人资料”页面。我们的小组在ASP.net中有这个东西,C#作为后端。他们已经提出了一些用于编辑个人资料信息到目前为止,页面看起来像这样:I added side menu

他们没有添加侧边菜单,但我做到了。我想要做的是,通过点击不同的选项,可以在屏幕上显示不同的元素。

我的ASP代码如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Profile.aspx.cs" Inherits="User_Profile" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <!-- Should this page be visible to only this user? -->
    <asp:Label ID="FirstNameLabel" runat="server" Text="First Name: "></asp:Label>&nbsp;&nbsp;&nbsp;
    <asp:Label ID="YourFirstNameLabel" runat="server"></asp:Label>
    <br />
    <asp:Label ID="ChangeFirstNameLabel" runat="server" Text="New First Name: "></asp:Label>
    <asp:TextBox ID="FirstNameTextBox" runat="server"></asp:TextBox>
    <br /><br />
    <asp:Label ID="LastNameLabel" runat="server" Text="Last Name: "></asp:Label>&nbsp;&nbsp;&nbsp;
    <asp:Label ID="YourLastNameLabel" runat="server"></asp:Label>
    <br />
    <asp:Label ID="ChangeLastNameLabel" runat="server" Text="New Last Name: "></asp:Label>
    <asp:TextBox ID="LastNameTextBox" runat="server"></asp:TextBox>
    <br /><br />
    <asp:Button ID="UpdateProfileButton" runat="server" OnClick="UpdateProfileButton_Click" Text="Update Profile" />
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="LeftContent" Runat="Server">
    <p style="height: 151px">
        <asp:ListBox 
            id="ProfileSideMenu"
            AutoPostBack="true"
            SelectionMode="single"
            onSelectedIndexChanged="ProfileSideMenu_SelectionChanged"
            runat="server">
            <asp:ListItem>Edit Profile</asp:ListItem>
            <asp:ListItem>View Profile</asp:ListItem>
            <asp:ListItem>View Friends</asp:ListItem>
        </asp:ListBox>
        <!-- This is dummy label and will be discarded when all is said and done -->
        <asp:Label ID="DummyLabel" runat="server"></asp:Label>
    </p>
</asp:Content>

我的想法是为&#34;编辑个人资料&#34;提供服务器控制。仅在&#34;编辑个人资料&#34;时显示被选中(最终如果loggedInUser是他们正在查看其个人资料的用户。在PHP中,这就像覆盖该元素的内容一样简单(通过echo)。但是,我不知道如何覆盖服务器控件。(我没有找到关于如何执行此操作的文档。)

到目前为止,我的C#代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class User_Profile : System.Web.UI.Page
{
    private static string EDIT_PROFILE = "Edit Profile",
        VIEW_PROFILE = "View Profile",
        VIEW_FRIENDS = "View Friends";

    protected void Page_Load(object sender, EventArgs e)
    {
        YourFirstNameLabel.Text = Profile.FirstName;
        YourLastNameLabel.Text = Profile.LastName;
    }

    protected void UpdateProfileButton_Click(object sender, EventArgs e)
    {
        Profile.FirstName = Server.HtmlEncode(FirstNameTextBox.Text);
        YourFirstNameLabel.Text = Profile.FirstName;

        Profile.LastName = Server.HtmlEncode(LastNameTextBox.Text);
        YourLastNameLabel.Text = Profile.LastName;
    }

    // to be called when selection changes in ProfileSideMenu
    protected void ProfileSideMenu_SelectionChanged(object sender, EventArgs e)
    {
        // get the text of the option selected
        string listSelection = "";
        if (ProfileSideMenu.SelectedIndex > -1)
        {
            listSelection = ProfileSideMenu.SelectedItem.Text;
        }
        // TODO: based on text, display the appropriate panel in #Content2
        if (listSelection != "")
        {
            // For right now, just display it in DummyLabel
            DummyLabel.Text = listSelection;
            if (listSelection == User_Profile.EDIT_PROFILE)
            {
                // TODO: Show only elements for "Edit Profile"
            }
            if (listSelection == User_Profile.VIEW_PROFILE)
            {
                // TODO: Show only elements for "View Profile"
            }
            if (listSelection == User_Profile.VIEW_FRIENDS)
            {
                // TODO: Show only elements for "View Friends"
            }
        }
        else
        {
            DummyLabel.Text = "No selection";
        }
    }
}

我是ASP.net的新手....

1 个答案:

答案 0 :(得分:1)

有许多不同的方法可以做到这一点,但一种选择是使用MultiView控件。 https://msdn.microsoft.com/en-us/library/ms227665.aspx

基本上,您会将每个部分定义为View并显示您想要的部分。

<asp:MultiView ID="mvProfile" runat="server">
    <asp:View ID="viewEditProfile" runat="server">
        <h3>Edit profile</h3>
        <%--- Edit profile controls ---%>           
    </asp:View>
    <asp:View ID="viewViewProfile" runat="server">
        <h3>View profile</h3>
        <%--- View profile controls ---%>                       
    </asp:View>
    <asp:View ID="viewViewFriends" runat="server">
        <h3>View Friends</h3>
        <%--- View friends profile controls ---%>           
    </asp:View>
</asp:MultiView>

接下来,调整if语句以更改ActiveViewIndex

if (listSelection == User_Profile.EDIT_PROFILE)
{
    // TODO: Show only elements for "Edit Profile"
    mvProfile.ActiveViewIndex = 0; // 0 for the first view
}
if (listSelection == User_Profile.VIEW_PROFILE)
{
    // TODO: Show only elements for "View Profile"
    mvProfile.ActiveViewIndex = 1; // 1 for the second view
}
if (listSelection == User_Profile.VIEW_FRIENDS)
{
    // TODO: Show only elements for "View Friends"
    mvProfile.ActiveViewIndex = 2; // 2 for the third view
}

有很多方法可以解决这个问题,但这只是一个例子。