在选择radiobutton列表时,我想查询数据库中的总数和余额。在名为 eleave 的数据库中,我有3个表年度,特殊,孕妇。
在表格中,每种类型的假期都有总和余额列。
<asp:RadioButtonList ID="rblleavetype" runat="server" CellPadding="0"
CellSpacing="0" style="text-align: left"
onselectedindexchanged="rblleavetype_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="al">Annualleave</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>
我已经在这个系统上工作了几个月了,我仍然无法找到解决方案。附:我仍然是C#世界的新人。
答案 0 :(得分:1)
您似乎不熟悉使用ASP.Net进行Web开发(或者您可能很懒。只是开玩笑:-D)。无论如何,希望这将有助于你和将来的某个人。
首先,您需要在AutoPostBack="true"
中拥有RadioButtonList
。然后,只有它会向服务器发送回发并调用rblleavetype_SelectedIndexChanged
事件。
好的,我创建了一个工作样本(代码是自解释的,我已经在必要时添加了注释)。我将根据单选按钮选择说明在html表中显示数据的步骤。
<强> STEPS 强>
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>
rblleavetype_SelectedIndexChanged
方法和一些逻辑来加载Page_Load
事件的初始值。使用System;
使用MyLeave;
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.cs
)使用System;
使用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
}
}