Мне поручено в моем групповом проекте создать страницу профиля для всех пользователей. В нашей группе это происходит в ASP.net, а C# - как backend. Они уже что-то придумали для редактирования информации профиля. Страница выглядит так: Условно отображение элементов управления сервером
Они не добавили боковое меню, но я это сделал. То, что я хочу сделать, - это возможность, нажав на другой параметр, отображать на экране различные элементы.
Мой 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>
<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>
<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>
Моя идея заключается в том, чтобы иметь серверные элементы управления для «Редактировать профиль» показать только тогда, когда «Редактировать профиль» определена (и в конечном счете, если 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";
}
}
}
Я всего Newb в ASP.net ....
Если это не ясно, я хочу показать только соответствующие элементы в 'Content2' –