webpart和用户的问题

wwonion 2007-02-25 01:47:11
有一个LIST.ASPX页面,用USER做参数显示不同的内容
如LIST.ASPX?USER=a 显示a用户的内容
如LIST.ASPX?USER=B 显示B用户的内容

现在问题是普通浏览者怎么浏览用户a和B设定的webpart布局和信息.

普通浏览者访问LIST.ASPX?USER=a 显示a设定的webpart布局和信息
普通浏览者访问LIST.ASPX?USER=B 显示b设定的webpart布局和信息

用户A和B怎么保存自己定义的布局,因为都是在用一个页面如何做到不冲突.
...全文
331 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
davidhpd 2008-05-20
  • 打赏
  • 举报
回复
我按照你的代码改了
但还是有问题
我用的是登录和不登录的状态
登录了自己可以更改自己的布局
然后也可以保存,只要一登录就是改过的布局
但如果一个用户不登录
显示的是默认的布局,而不是更改过的布局
我想让用户不登录也显示是更改过的布局

谢谢楼主!!
可与我联系:QQ46161681
MSN:houpeidong@msn.com
wwonion 2007-02-26
  • 打赏
  • 举报
回复
问题基本解决,分享下

继承PersonalizationProvider,重写一些方法

代码如下

using System;
using System.Configuration.Provider;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using DBClass;//自己的SQL类
using System.Data;
public class TextFilePersonalizationProvider : PersonalizationProvider
{
public override string ApplicationName
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}

public override void Initialize(string name,
NameValueCollection config)
{
// Verify that config isn't null
if (config == null)
throw new ArgumentNullException("config");

// Assign the provider a default name if it doesn't have one
if (String.IsNullOrEmpty(name))
name = "TextFilePersonalizationProvider";

// Add a default "description" attribute to config if the
// attribute doesn't exist or is empty
if (string.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description",
"Text file personalization provider");
}

// Call the base class's Initialize method
base.Initialize(name, config);

// Throw an exception if unrecognized attributes remain
if (config.Count > 0)
{
string attr = config.GetKey(0);
if (!String.IsNullOrEmpty(attr))
throw new ProviderException
("Unrecognized attribute: " + attr);
}

// Make sure we can read and write files in the
// ~/Personalization_Data directory
FileIOPermission permission = new FileIOPermission
(FileIOPermissionAccess.AllAccess,
HttpContext.Current.Server.MapPath
("~/Personalization_Data"));
permission.Demand();
}



protected override void LoadPersonalizationBlobs(WebPartManager webPartManager, string path, string userName, ref byte[] sharedDataBlob, ref byte[] userDataBlob)
{
//从自己定义的数据表中读取数据
sharedDataBlob = null;
//userDataBlob = Convert.FromBase64String(reader1.ReadLine());
string shopid = HttpContext.Current.Request["shopid"];
if (shopid == null)
{
userDataBlob = null;
return;
}
SqlConn db = new SqlConn();//自己的SQL类
db.dbname = "efushop";
string tempstr = db.Value("SELECT code FROM WebPart WHERE (url = '" + path + "') AND (shopid = '" + shopid + "')");
if (tempstr!=null)
userDataBlob = Convert.FromBase64String(tempstr);


}

protected override void ResetPersonalizationBlob
(WebPartManager webPartManager, string path, string userName)
{

// Delete the specified personalization file
try
{
string shopid = HttpContext.Current.Request["shopid"];
if (shopid == null)
{

return;
}
SqlConn db = new SqlConn();
db.dbname = "efushop";
db.Exec("Delete FROM WebPart WHERE (url = '" + path + "') AND (shopid = '" + shopid + "')");
}
catch (FileNotFoundException) { }
}


protected override void SavePersonalizationBlob(WebPartManager webPartManager, string path, string userName,byte[] dataBlob)
{
//保存到自己定义的数据表中数据
string shopid = HttpContext.Current.Request["shopid"];
if (shopid == null)
{

return;
}
SqlConn db = new SqlConn();
db.dbname = "efushop";
DataTable dt = db.Dt("SELECT id FROM WebPart WHERE (url = '" + path + "') AND (shopid = '" + shopid + "')");
if (dt.Rows.Count > 0)
{
string fields = "code,addtime";
string[] val = new string[2];
val[0] = Convert.ToBase64String(dataBlob);
val[1] = DateTime.Now.ToString();
db.Update("webpart", fields, val, "id=" + dt.Rows[0]["id"].ToString());
}
else
{
string fields = "code,shopid,url";
string[] val = new string[3];
val[0] = Convert.ToBase64String(dataBlob);
val[1] = shopid;
val[2] = path;
db.Insert("webpart", fields, val);
}



}

public override PersonalizationStateInfoCollection FindState
(PersonalizationScope scope, PersonalizationStateQuery query,
int pageIndex, int pageSize, out int totalRecords)
{
throw new NotSupportedException();
}

public override int GetCountOfState(PersonalizationScope scope,
PersonalizationStateQuery query)
{
throw new NotSupportedException();
}

public override int ResetState(PersonalizationScope scope,
string[] paths, string[] usernames)
{
throw new NotSupportedException();
}

public override int ResetUserState(string path,
DateTime userInactiveSinceDate)
{
throw new NotSupportedException();
}

}


然后在 web.config 中指定
<webParts>
<personalization defaultProvider="AspNetTextFilePersonalizationProvider">
<providers>
<add name="AspNetTextFilePersonalizationProvider" type="TextFilePersonalizationProvider"/>
</providers>
<authorization>
<allow users="*" verbs="enterSharedScope"/>
</authorization>
</personalization>
</webParts>

基本上就可以实现同一页面按不同的参数显示不同的布局.

有问题的请跟帖提问.

我知道的尽量回答.
liutianyuzj 2007-02-25
  • 打赏
  • 举报
回复
学习中...
wwonion 2007-02-25
  • 打赏
  • 举报
回复
楼上的答案不是我想要的结果.

我想知道webpart的实现
huangkc 2007-02-25
  • 打赏
  • 举报
回复
你可先分个类,如有几种布局
每个类对应一个Control集合(对应一个页面)
然后在页面中根据user值判断属于哪一类然后显示

还可以用内嵌形式,iframe

也可以考虑用Master Page (vs2005中)

wwonion 2007-02-25
  • 打赏
  • 举报
回复
再次

召唤高手

帮顶有分
wwonion 2007-02-25
  • 打赏
  • 举报
回复
召唤高手

帮顶有分

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧