看来您是使用 ASP.Net 进行 Web 开发的新手(或者您可能很懒惰。开个玩笑 :-D)。无论如何,希望这对您和将来的某人有所帮助。
首先你需要AutoPostBack="true"
在你的RadioButtonList
. 然后只有它会向服务器发送回发并调用rblleavetype_SelectedIndexChanged
事件。
好的,我创建了一个工作示例(代码是不言自明的,我在必要时添加了注释)。我将解释基于单选按钮选择在 html 表中显示数据的步骤。
脚步
- 创建一个带有 RabioButtonList 和表格的网页(我称之为
LeaveBalance.aspx
)(正如您已经完成的那样)。
<form id="frmLeaves" runat="server">
<div>
<asp:RadioButtonList ID="rblleavetype" runat="server" CellPadding="0" CellSpacing="0" style="text-align: left"
onselectedindexchanged="rblleavetype_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Selected="True" Value="al">Annual leave</asp:ListItem>
<asp:ListItem Value="ml">Maternity leave</asp:ListItem>
<asp:ListItem Value="sp">Special</asp:ListItem>
</asp:RadioButtonList>
<table cellpadding="5" cellspacing="0" style="border: 1px solid #000000; width: 400px">
<tr>
<td style="border-right: 1px solid #000000; background-color: #D39A05; height: 25px; width: 200px; text-align: center; vertical-align: top;">
Total days for this leave type</td>
<td
style="border-left: 1px solid #000000; text-align: center;
background-color: #D39A05; height: 25px; width: 200px; vertical-align: top;">
Balance days available for this leave type</td>
</tr>
<tr>
<td style="border-right: 1px solid #000000; text-align: center; width: 200px; vertical-align: top;">
<asp:Label ID="ltotalleavetype" runat="server" CssClass="label"></asp:Label>
days</td>
<td style="border-left: 1px solid #000000; text-align: center; vertical-align: top; width: 200px;">
<asp:Label ID="lbalanceleavetype" runat="server" CssClass="label"></asp:Label>
days</td>
</tr>
</table>
</div>
</form>
- 然后在您的代码隐藏文件(LeaveBalance.aspx.cs)中,您应该定义您的方法和一些逻辑以在事件
rblleavetype_SelectedIndexChanged
中加载初始值。Page_Load
使用系统;
使用我的休假;
public partial class LeaveBalance : System.Web.UI.Page
{
// This is your UI logic.
/// <summary>
/// Create a private variable to hold all your leave retrieved from the database
/// I assume you have you'll have one row for each leave type
/// </summary>
public LeaveCollection AllLeaves {
get
{
if (ViewState["AllLeaves"] != null)
{
return (LeaveCollection)ViewState["AllLeaves"];
}
return new LeaveCollection();
}
set
{
// You need to save the collection in ViewState to persist the data
// Otherwise you'll loose all values AllLeaves will be reset in every postback
ViewState["AllLeaves"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetAllLeavesFromDatabase();
}
// I assume that annual leave radio option will be selected initially when the page loads
LoadDisplayTable(LeaveType.AL);
}
protected void rblleavetype_SelectedIndexChanged(object sender, EventArgs e)
{
LeaveType type = LeaveType.AL;
switch (rblleavetype.SelectedValue)
{
case "ml":
type = LeaveType.ML;
break;
case "sp":
type = LeaveType.SP;
break;
}
LoadDisplayTable(type);
}
/// <summary>
/// Gets all leaves from database.
/// </summary>
private void GetAllLeavesFromDatabase()
{
AllLeaves = new LeaveCollection();
/* At this point you should know how to retrieve your leave data from DB and fill the AllLeaves collection
E.g.
AllLeaves = DalService.GetAllLeavesFromDatabase(); // DalService could be your Data Access layer and GetAllLeavesFromDatabase() is one of it's methods
I'll be creating some dummy logic to fill the collection for demo purpose from this point onwards
*/
// Add annual leave to the collection
Leave al = new Leave(LeaveType.AL);
al.TotalDays = 15;
al.Available = 10;
AllLeaves.Add(al);
// Add Maternity leave
Leave ml = new Leave(LeaveType.ML);
ml.TotalDays = 60;
ml.Available = 5;
AllLeaves.Add(ml);
// Add Special leave
Leave sl = new Leave(LeaveType.SP);
sl.TotalDays = 5;
sl.Available = 3;
AllLeaves.Add(sl);
}
private void LoadDisplayTable(LeaveType type)
{
Leave selectedLeave = new Leave(type);
if (AllLeaves != null && AllLeaves.Count > 0)
{
// Here we find the leave object in the collection based on the leave type
selectedLeave = AllLeaves.Find(a => a.LeaveType == type);
}
// Populate your labels with selected leave type
ltotalleavetype.Text = selectedLeave.TotalDays.ToString();
lbalanceleavetype.Text = selectedLeave.Available.ToString();
}
}
- Leave 类用于保存请假数据 (
Leave.cs
)
使用系统;
使用 System.Collections.Generic;
namespace MyLeave
{
// This is your modal. It defines how you'd hold your leave data in memory
// This collection will hold all your leaves
[Serializable]
public class LeaveCollection : List<Leave> { }
// Base class to create your leave
[Serializable] // Since you are saving the object in ViewState you need to mark this class as Serializable
public class Leave
{
public LeaveType LeaveType { get; set; }
public int TotalDays { get; set; }
public int Available { get; set; }
public Leave(LeaveType type)
{
this.LeaveType = type;
this.TotalDays = 0;
this.Available = 0;
}
}
// This Enum will hold the leave type
public enum LeaveType
{
AL = 1,
ML = 2,
SP = 3
}
}