我目前正在为我的网站开发管理页面。目前,我正在尝试开发用户名册和规则。
我的数据库结构如下:
tblSiteUserRolls
id
rollName
rollDescription
tblSiteRollAccess
id
accessName
accessDescription
tblSiteRollLink
id
rollId
rollAccessId
allowed
tblSiteMemberDetail
~stuff~
userRollId
我手动添加了rolls
and rollAccess
,并将它们显示在Repeater
控件上,名称旁边有小复选框。这一切目前都完美无缺。
这可以通过将所有的添加rollAccess
到一个通用列表中,并将所有的值设置为allowed
0。然后我检查从tblSiteRollLinkrollAccess
中选择 的所有的,如果它在那里,设置为 1。roll
allowed
有没有一种快速的方法可以使用编辑的值插入或更新tblSiteRollLink中的记录,而不是遍历我的所有值repeater
并手动执行它?
这是一些代码:Page.aspx
<asp:Repeater ID="rptRollRules" runat="server">
<HeaderTemplate>
Roll Rules
<table>
<tr>
<th>Access Name
</th>
<th>Allowed
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblRollId" runat="server" Text='<%# Eval("RollId") %>' Visible="False"></asp:Label>
<asp:Label ID="lblAccessId" runat="server" Text='<%# Eval("AccessId") %>' Visible="False"></asp:Label>
<asp:Label ID="lblAccessName" runat="server" Text='<%# Eval("AccessName") %>'></asp:Label>
</td>
<td>
<asp:CheckBox ID="chkIsAllowed" runat="server" Checked='<%# Eval("AccessAllowed") %>'/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
页面.cs
private void PopulateRollRulesRepeater(string rollId)
{
// Get all the access rolls
List<RollAccess> access = new List<RollAccess>();
string strSql = "SELECT [id]" +
", [accessName]" +
", [accessDescription]" +
" FROM tblSiteRollAccess";
SqlCommand sqlComm = new SqlCommand(strSql, DataConn.Connect()) { CommandType = CommandType.Text };
SqlDataReader rdr = sqlComm.ExecuteReader();
while (rdr.Read())
{
access.Add(new RollAccess
{
RollId = rollId,
AccessId = rdr["id"],
AccessName = rdr["accessName"],
AccessDescription = rdr["accessDescription"],
AccessAllowed = false
});
}
rdr.Close();
DataConn.Disconnect();
// Check if the current roll has the access
strSql = "SELECT [rollAccessId]" +
", [allowed]" +
" FROM vwGetRollAccess WHERE rollID = @id AND allowed = 1";
sqlComm = new SqlCommand(strSql, DataConn.Connect()) { CommandType = CommandType.Text };
sqlComm.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = rollId;
rdr = sqlComm.ExecuteReader();
while (rdr.Read())
{
foreach (RollAccess rollAccess in access.Where(rollAccess => rollAccess.AccessId.ToString() == rdr["rollAccessId"].ToString()))
{
rollAccess.AccessAllowed = true;
}
}
rdr.Close();
DataConn.Disconnect();
rptRollRules.DataSource = access;
rptRollRules.DataBind();
}
public class RollAccess
{
public object RollId { get; set; }
public object AccessId { get; set; }
public object AccessName { get; set; }
public object AccessDescription { get; set; }
public object AccessAllowed { get; set; }
}
/// <summary>
/// Update the Roll's details and access levels
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpdate_Click(object sender, EventArgs e)
{
DataTable rules = new DataTable();
DataColumn rollId = new DataColumn("rollId", typeof(int));
DataColumn rollAccessId = new DataColumn("rollAccessId", typeof(int));
DataColumn allowed = new DataColumn("allowed", typeof(int));
rules.Columns.Add(rollId);
rules.Columns.Add(rollAccessId);
rules.Columns.Add(allowed);
DataRow drRules = rules.NewRow();
// Update the access levels
foreach (RepeaterItem i in rptRollRules.Items)
{
//Retrieve the state of the CheckBox
CheckBox chkIsAllowed = (CheckBox) i.FindControl("chkIsAllowed");
Label lblRollId = (Label) i.FindControl("lblRollId");
Label lblAccessId = (Label) i.FindControl("lblAccessId");
drRules["rollId"] = lblRollId.Text;
drRules["rollAccessId"] = lblAccessId.Text;
drRules["allowed"] = chkIsAllowed.Checked.GetHashCode();
}
string strSql = "";
}
/当前/ SQL
-- =============================================
-- Author: Darren Whitfield
-- Create date: 24 April 2013
-- Description: This will update the user roll rules
-- =============================================
CREATE TABLE [rulesTable]
(
[ID] [int] NOT NULL PRIMARY KEY IDENTITY,
[rollId] [int] NOT NULL,
[rollAccessId] [int] NOT NULL,
[allowed] [tinyint] NOT NULL
)
CREATE TYPE [rulesUDT] AS TABLE
(
[rollId] [int] NOT NULL,
[rollAccessId] [int] NOT NULL,
[allowed] [tinyint] NOT NULL
)
GO
CREATE PROCEDURE procUpdateUserRollRules
@tbl rulesUDT READONLY
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [rulesTable] SELECT * FROM @tbl
END
GO