2008年1月小记
1、使用List<T>.ConvertAll方法
2、CheckBox全选例子。
3、使用svcutil 生成WCF客户端
打开Visual Studio 2008 Command Prompt
d:
cd temp
svcutil /language:c# /out:NotifyServiceClient.cs /config:app.config http://localhost:8000/Service/NotifyService.svc
4、手工书写WCF客户端
5、如何方便地自动生成WCF客户端及配置文件。
A、设置VS2008 X86 tools的环境:Call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
B、svcutil /language:c# /out:NotifyServiceClient.cs /config:app.config http://localhost:8000/Service/NotifyService.svc
C、写成一个生成后事件
public List<string> GetDisallowedUserNames()
{
List<DisallowedUser> list = GetDisallowedUsers();
return list.ConvertAll<string>(new Converter<DisallowedUser, string>(delegate(DisallowedUser u) { return u.UserName; }));
}
{
List<DisallowedUser> list = GetDisallowedUsers();
return list.ConvertAll<string>(new Converter<DisallowedUser, string>(delegate(DisallowedUser u) { return u.UserName; }));
}
[Serializable]
public class DisallowedUser
{
private string userName;
private DateTime createdDate;
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get { return userName; }
set { userName = value; }
}
/// <summary>
/// 添加时间
/// </summary>
public DateTime CreatedDate
{
get { return createdDate; }
set { createdDate = value; }
}
}
public class DisallowedUser
{
private string userName;
private DateTime createdDate;
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get { return userName; }
set { userName = value; }
}
/// <summary>
/// 添加时间
/// </summary>
public DateTime CreatedDate
{
get { return createdDate; }
set { createdDate = value; }
}
}
2、CheckBox全选例子。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisallowedUserList.aspx.cs" Inherits="Admin_DisallowedUserList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>禁用用户</title>
<link href="Styles/BaseStyle.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
// <![CDATA[
function doSelectThis()
{
var flag = true;
var objs = getCheckBoxs();
for(i=0; i<objs.length; i++)
{
//只要有一项没有选择,则不能设置为全选
if(objs[i].checked == false)
{
flag = false;
break;
}
}
var obj = document.getElementById('chbSelectAll');
if(flag)
{
obj.checked = true;
}
else
{
obj.checked = false;
}
}
function doSelectAll()
{
var objs = getCheckBoxs();
for(i=0; i<objs.length; i++)
{
objs[i].checked = isCheckAll() ? true : false;
}
}
function isCheckAll()
{
var obj = document.getElementById('chbSelectAll');
return obj.checked;
}
function getCheckBoxs()
{
var ids = document.getElementById('<%= hfClientIDs.ClientID %>').value + ']';
var idArray = eval('(' + ids + ')');
var objs = new Array();
for(i=0; i<idArray.length; i++)
{
objs.push(document.getElementById(idArray[i]));
}
return objs;
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="margin:0; padding:10px 0 10px 0;">
用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:Button ID="btnInsert" runat="server" Text="禁用" CssClass="defaultButton" OnClick="btnInsert_Click" />
<asp:HiddenField ID="hfClientIDs" runat="server" />
<asp:LinkButton ID="lbtnDelete" runat="server" OnClick="lbtnDelete_Click" OnClientClick="return confirm('确定要解禁你选择的用户吗?');">解禁选定</asp:LinkButton>
</div>
<asp:Repeater ID="retDisallowedUsers" runat="server" OnItemDataBound="retDisallowedUsers_ItemDataBound">
<HeaderTemplate>
<table class="baseTable" cellpadding="0" cellspacing="0" border="0" width="500px">
<thead>
<tr>
<td style="width:80px;">
<input id="chbSelectAll" onfocus="this.blur()" onclick="doSelectAll()" value="全选" type="checkbox" /><a id="lblCheckall" >全选</a>
</td>
<td>被禁用用户</td>
<td>禁用时间</td>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><asp:CheckBox ID="chbSelectedUser" runat="server" /></td>
<td><asp:Literal ID="litUserName" runat="server" /></td>
<td><asp:Literal ID="litCreatedDate" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>禁用用户</title>
<link href="Styles/BaseStyle.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
// <![CDATA[
function doSelectThis()
{
var flag = true;
var objs = getCheckBoxs();
for(i=0; i<objs.length; i++)
{
//只要有一项没有选择,则不能设置为全选
if(objs[i].checked == false)
{
flag = false;
break;
}
}
var obj = document.getElementById('chbSelectAll');
if(flag)
{
obj.checked = true;
}
else
{
obj.checked = false;
}
}
function doSelectAll()
{
var objs = getCheckBoxs();
for(i=0; i<objs.length; i++)
{
objs[i].checked = isCheckAll() ? true : false;
}
}
function isCheckAll()
{
var obj = document.getElementById('chbSelectAll');
return obj.checked;
}
function getCheckBoxs()
{
var ids = document.getElementById('<%= hfClientIDs.ClientID %>').value + ']';
var idArray = eval('(' + ids + ')');
var objs = new Array();
for(i=0; i<idArray.length; i++)
{
objs.push(document.getElementById(idArray[i]));
}
return objs;
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="margin:0; padding:10px 0 10px 0;">
用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:Button ID="btnInsert" runat="server" Text="禁用" CssClass="defaultButton" OnClick="btnInsert_Click" />
<asp:HiddenField ID="hfClientIDs" runat="server" />
<asp:LinkButton ID="lbtnDelete" runat="server" OnClick="lbtnDelete_Click" OnClientClick="return confirm('确定要解禁你选择的用户吗?');">解禁选定</asp:LinkButton>
</div>
<asp:Repeater ID="retDisallowedUsers" runat="server" OnItemDataBound="retDisallowedUsers_ItemDataBound">
<HeaderTemplate>
<table class="baseTable" cellpadding="0" cellspacing="0" border="0" width="500px">
<thead>
<tr>
<td style="width:80px;">
<input id="chbSelectAll" onfocus="this.blur()" onclick="doSelectAll()" value="全选" type="checkbox" /><a id="lblCheckall" >全选</a>
</td>
<td>被禁用用户</td>
<td>禁用时间</td>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><asp:CheckBox ID="chbSelectedUser" runat="server" /></td>
<td><asp:Literal ID="litUserName" runat="server" /></td>
<td><asp:Literal ID="litCreatedDate" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CSDN.ServiceComponents.PermissionWrapper;
using CSDN.Profile.IBLL;
using CSDN.Profile.Components;
using System.Collections.Generic;
using CSDN.Profile.ComponentModel;
using CSDN.Profile.Lib;
public partial class Admin_DisallowedUserList : System.Web.UI.Page
{
protected string UserName = "";
protected void Page_Load(object sender, EventArgs e)
{
IUserExtension iUserExtension = ObjectFactory.CreateIUserExtension();
if (!iUserExtension.IsLogin(out UserName))
{
SiteUrls siteUrls = SiteUrls.Instance();
Response.Redirect(siteUrls.UserLogin(Request.RawUrl));
}
if (!HavePermission("select,insert,update,delete"))
{
Response.Output.Write("你没有访问此页面的相应的操作权限");
Response.End();
}
if (!IsPostBack)
{
this.BindDisallowedUsers();
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{
string txt = txtUserName.Text.Trim();
string[] names = txt.Split(',');
IDisallowedUser iDisallowedUser = ObjectFactory.CreateIDisallowedUser();
foreach (string name in names)
{
iDisallowedUser.AddDisallowedUser(name.Trim());
}
Response.Redirect(Request.RawUrl, true);
}
protected void retDisallowedUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DisallowedUser disallowedUser = e.Item.DataItem as DisallowedUser;
if (disallowedUser != null)
{
Literal litUserName = (Literal)e.Item.FindControl("litUserName");
Literal litCreatedDate = (Literal)e.Item.FindControl("litCreatedDate");
CheckBox chbSelectedUser = (CheckBox)e.Item.FindControl("chbSelectedUser");
litUserName.Text = disallowedUser.UserName;
litCreatedDate.Text = disallowedUser.CreatedDate.ToString();
//把当前的CheckBox的ClientID的值填充到hfClientIDs中
if (String.IsNullOrEmpty(hfClientIDs.Value))
{
//注意:"[\""与JS代码function getCheckBoxs()中的']'对应,共同形式一个JSON字符串
hfClientIDs.Value = "[\"" + chbSelectedUser.ClientID + "\"";
}
else
{
hfClientIDs.Value += String.Format(",\"{0}\"", chbSelectedUser.ClientID);
}
//设置CheckBox的客户端脚本
chbSelectedUser.Attributes.Add("onclick", "doSelectThis()");
}
}
}
protected void lbtnDelete_Click(object sender, EventArgs e)
{
IDisallowedUser iDisallowedUser = ObjectFactory.CreateIDisallowedUser();
List<string> list = GetSelecedUserNames();
foreach (string name in list)
{
iDisallowedUser.RemoveDisallowedUser(name);
}
Response.Redirect(Request.RawUrl, true);
}
private void BindDisallowedUsers()
{
List<DisallowedUser> list = ObjectFactory.CreateIDisallowedUser().GetDisallowedUsers();
retDisallowedUsers.DataSource = list;
retDisallowedUsers.DataBind();
}
private List<string> GetSelecedUserNames()
{
List<String> userNames = new List<string>();
foreach (RepeaterItem item in retDisallowedUsers.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CheckBox chbSelectedUser = (CheckBox)item.FindControl("chbSelectedUser");
Literal litUserName = (Literal)item.FindControl("litUserName");
if (chbSelectedUser.Checked)
{
userNames.Add(litUserName.Text.Trim());
}
}
}
return userNames;
}
private bool HavePermission(string permissions)
{
string applicationId = ConfigurationManager.AppSettings["ApplicationId"];
string targetId = "b6adda71-e3fb-4f27-bdb5-c201bdbabf77";
string clientId = ConfigurationManager.AppSettings["ClientId"];
string clientKey = ConfigurationManager.AppSettings["ClientKey"];
return PermissionServices.HavePermission(clientId, clientKey, applicationId, targetId, this.UserName, permissions);
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CSDN.ServiceComponents.PermissionWrapper;
using CSDN.Profile.IBLL;
using CSDN.Profile.Components;
using System.Collections.Generic;
using CSDN.Profile.ComponentModel;
using CSDN.Profile.Lib;
public partial class Admin_DisallowedUserList : System.Web.UI.Page
{
protected string UserName = "";
protected void Page_Load(object sender, EventArgs e)
{
IUserExtension iUserExtension = ObjectFactory.CreateIUserExtension();
if (!iUserExtension.IsLogin(out UserName))
{
SiteUrls siteUrls = SiteUrls.Instance();
Response.Redirect(siteUrls.UserLogin(Request.RawUrl));
}
if (!HavePermission("select,insert,update,delete"))
{
Response.Output.Write("你没有访问此页面的相应的操作权限");
Response.End();
}
if (!IsPostBack)
{
this.BindDisallowedUsers();
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{
string txt = txtUserName.Text.Trim();
string[] names = txt.Split(',');
IDisallowedUser iDisallowedUser = ObjectFactory.CreateIDisallowedUser();
foreach (string name in names)
{
iDisallowedUser.AddDisallowedUser(name.Trim());
}
Response.Redirect(Request.RawUrl, true);
}
protected void retDisallowedUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DisallowedUser disallowedUser = e.Item.DataItem as DisallowedUser;
if (disallowedUser != null)
{
Literal litUserName = (Literal)e.Item.FindControl("litUserName");
Literal litCreatedDate = (Literal)e.Item.FindControl("litCreatedDate");
CheckBox chbSelectedUser = (CheckBox)e.Item.FindControl("chbSelectedUser");
litUserName.Text = disallowedUser.UserName;
litCreatedDate.Text = disallowedUser.CreatedDate.ToString();
//把当前的CheckBox的ClientID的值填充到hfClientIDs中
if (String.IsNullOrEmpty(hfClientIDs.Value))
{
//注意:"[\""与JS代码function getCheckBoxs()中的']'对应,共同形式一个JSON字符串
hfClientIDs.Value = "[\"" + chbSelectedUser.ClientID + "\"";
}
else
{
hfClientIDs.Value += String.Format(",\"{0}\"", chbSelectedUser.ClientID);
}
//设置CheckBox的客户端脚本
chbSelectedUser.Attributes.Add("onclick", "doSelectThis()");
}
}
}
protected void lbtnDelete_Click(object sender, EventArgs e)
{
IDisallowedUser iDisallowedUser = ObjectFactory.CreateIDisallowedUser();
List<string> list = GetSelecedUserNames();
foreach (string name in list)
{
iDisallowedUser.RemoveDisallowedUser(name);
}
Response.Redirect(Request.RawUrl, true);
}
private void BindDisallowedUsers()
{
List<DisallowedUser> list = ObjectFactory.CreateIDisallowedUser().GetDisallowedUsers();
retDisallowedUsers.DataSource = list;
retDisallowedUsers.DataBind();
}
private List<string> GetSelecedUserNames()
{
List<String> userNames = new List<string>();
foreach (RepeaterItem item in retDisallowedUsers.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CheckBox chbSelectedUser = (CheckBox)item.FindControl("chbSelectedUser");
Literal litUserName = (Literal)item.FindControl("litUserName");
if (chbSelectedUser.Checked)
{
userNames.Add(litUserName.Text.Trim());
}
}
}
return userNames;
}
private bool HavePermission(string permissions)
{
string applicationId = ConfigurationManager.AppSettings["ApplicationId"];
string targetId = "b6adda71-e3fb-4f27-bdb5-c201bdbabf77";
string clientId = ConfigurationManager.AppSettings["ClientId"];
string clientKey = ConfigurationManager.AppSettings["ClientKey"];
return PermissionServices.HavePermission(clientId, clientKey, applicationId, targetId, this.UserName, permissions);
}
}
3、使用svcutil 生成WCF客户端
打开Visual Studio 2008 Command Prompt
d:
cd temp
svcutil /language:c# /out:NotifyServiceClient.cs /config:app.config http://localhost:8000/Service/NotifyService.svc
4、手工书写WCF客户端
public class CacheNotifiedUserClient : ClientBase<ICacheNotifiedUser>, ICacheNotifiedUser
{
public CacheNotifiedUserClient() : base() { }
public CacheNotifiedUserClient(InstanceContext callbackInstance) : base(callbackInstance) { }
public CacheNotifiedUserClient(string endpointConfigurationName) : base(endpointConfigurationName) { }
public CacheNotifiedUserClient(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { }
public CacheNotifiedUserClient(InstanceContext callbackInstance, string endpointConfigurationName) : base(callbackInstance, endpointConfigurationName) { }
public CacheNotifiedUserClient(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
public CacheNotifiedUserClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
public CacheNotifiedUserClient(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress) : base(callbackInstance, binding, remoteAddress) { }
public CacheNotifiedUserClient(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress) : base(callbackInstance, endpointConfigurationName, remoteAddress) { }
public CacheNotifiedUserClient(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) : base(callbackInstance, endpointConfigurationName, remoteAddress) { }
public void Dispose() { this.Close();}
public bool Contains(string userName)
{
return this.Channel.Contains(userName);
}
}
{
public CacheNotifiedUserClient() : base() { }
public CacheNotifiedUserClient(InstanceContext callbackInstance) : base(callbackInstance) { }
public CacheNotifiedUserClient(string endpointConfigurationName) : base(endpointConfigurationName) { }
public CacheNotifiedUserClient(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { }
public CacheNotifiedUserClient(InstanceContext callbackInstance, string endpointConfigurationName) : base(callbackInstance, endpointConfigurationName) { }
public CacheNotifiedUserClient(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
public CacheNotifiedUserClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
public CacheNotifiedUserClient(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress) : base(callbackInstance, binding, remoteAddress) { }
public CacheNotifiedUserClient(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress) : base(callbackInstance, endpointConfigurationName, remoteAddress) { }
public CacheNotifiedUserClient(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) : base(callbackInstance, endpointConfigurationName, remoteAddress) { }
public void Dispose() { this.Close();}
public bool Contains(string userName)
{
return this.Channel.Contains(userName);
}
}
5、如何方便地自动生成WCF客户端及配置文件。
A、设置VS2008 X86 tools的环境:Call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
B、svcutil /language:c# /out:NotifyServiceClient.cs /config:app.config http://localhost:8000/Service/NotifyService.svc
C、写成一个生成后事件