自定义webpart部件问题

forum2621 2009-12-01 05:05:33
小弟自定义一个webpart,用来读取列表数据并绑定到gridview上,一直通不过去,页面提示:值不在预期的范围内。
不清楚到底错在哪了,请各位指导下,谢谢了。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
using System.Data;
using System.Xml.Serialization;
namespace MyWebPart
{

[XmlRoot(Namespace = "MyWebPart")]
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebPartTest runat=server></{0}:WebPartTest>")]


public class WebPartTest : WebPart
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]


protected Label label;
protected GridView gridview;


public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? "[" + this.ID + "]" : s);
}

set
{
ViewState["Text"] = value;
}
}
protected override void RenderWebPart(HtmlTextWriter output)
{
//base.RenderWebPart(output);
RenderChildren(output);
}
protected override void CreateChildControls()
{
//base.CreateChildControls();

label = new Label();
label.Text = "This is a webpart test...";

Controls.Add(label);

gridview = new GridView();
gridview.Load += new EventHandler(GridViewLoad);


gridview.AutoGenerateColumns = true;

Controls.Add(gridview);

}
public void GridViewLoad(object sender, EventArgs e)
{
BindGridView();
}

private void BindGridView()
{
using (SPWeb web = SPContext.Current.Web)
{
SPList list = web.Lists["列表001"];
if (list != null)
{
SPQuery spquery = new SPQuery();
gridview.DataSource = list.GetItems(spquery).GetDataTable();
gridview.DataBind();

}
else
{

// label.Text = "no list....";
}

}

throw new NotImplementedException();
}

}
}


...全文
279 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
forum2621 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 time_is_life 的回复:]
没看到你的分页处理事件。另外不知道你为什么去掉using,而用那么奇怪的写法。
[/Quote]
分页是在其它方法里边,这个地方只是绑数据。

[Quote=引用 9 楼 forever_kingdom 的回复:]
SPContext.Current.Web这个对象是一个全局的,不应该dispose这个对象
如果是SPGridView的分页可以看看这个http://blog.csdn.net/forever_kingdom/archive/2009/11/25/4873724.aspx
[/Quote]
如果不dispose的话,我的结果就是正确的了,多谢了啊。
再问下,sharepoint里边可以绑定多个list到一个gridview中吗?类似 sql的 inner join等,我找了半天 也没找到相关资料,有的方法只是 用 spd来实现的 ,我想后台写代码来实现,不清楚能实现不 。
下面这个是用spd做的 http://office.microsoft.com/en-us/sharepointdesigner/HA103511401033.aspx
forum2621 2009-12-02
  • 打赏
  • 举报
回复
遇到新问题了:gridview如果分页的话一直报:正在尝试使用已关闭或释放并且不再有效的 SPWeb 对象。
把代码改为如下(去掉using,并不调用web.Dispose())就不会报错,请问有设么解决办法来避免这个问题。

private void BindGridView()
{

//using
SPWeb web = SPContext.Current.Web;
{

SPList list = null;
try
{
list = web.Lists["列表001"];

}
catch
{
label.Text = "No List Found...";
}
if (list != null)
{
//SPQuery spquery = new SPQuery();
gridview.DataSource = list.Items.GetDataTable();
gridview.DataBind();

}
else
{

// label.Text = "no list....";
}

}

//web.Dispose();
}

  • 打赏
  • 举报
回复
SPContext.Current.Web这个对象是一个全局的,不应该dispose这个对象
如果是SPGridView的分页可以看看这个http://blog.csdn.net/forever_kingdom/archive/2009/11/25/4873724.aspx
Justin-Liu 2009-12-02
  • 打赏
  • 举报
回复
jf
time_is_life 2009-12-02
  • 打赏
  • 举报
回复
没看到你的分页处理事件。另外不知道你为什么去掉using,而用那么奇怪的写法。
time_is_life 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 forum2621 的回复:]
找不到list就报这个错误,找到了会正常显示....
System.Exception: 引发类型为“System.Exception”的异常。 在 MyWebPart.WebPartTest.BindGridView() 在 MyWebPart.WebPartTest.GridViewLoad(Object sender, EventArgs e) 在 System.Web.UI.Control.OnLoad(EventArgs e) 在 System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e) 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Control.AddedControl(Control control, Int32 index) 在 System.Web.UI.ControlCollection.Add(Control child) 在 MyWebPart.WebPartTest.CreateChildControls()

[/Quote]

就是这样的,因为用的集合,找不到元素肯定报这样的错误。
楼上的方法正确。
我想这个moss的一个缺陷,这种判断应该用api来做的。只要给我们返回个null就可以了。
  • 打赏
  • 举报
回复
应该是这句代码的问题
SPList list = web.Lists["列表001"];
你可以使用下面的方式,然后再判断
SPList list = null;
try
{
list = web.Lists["列表001"];
}
catch{}
if(list != null)
{
//......
}
forum2621 2009-12-01
  • 打赏
  • 举报
回复
找不到list就报这个错误,找到了会正常显示....
System.Exception: 引发类型为“System.Exception”的异常。 在 MyWebPart.WebPartTest.BindGridView() 在 MyWebPart.WebPartTest.GridViewLoad(Object sender, EventArgs e) 在 System.Web.UI.Control.OnLoad(EventArgs e) 在 System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e) 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Control.AddedControl(Control control, Int32 index) 在 System.Web.UI.ControlCollection.Add(Control child) 在 MyWebPart.WebPartTest.CreateChildControls()
forum2621 2009-12-01
  • 打赏
  • 举报
回复
应该是这个错误
System.ArgumentException: 值不在预期的范围内。 在 Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean bThrowException) 在 Microsoft.SharePoint.SPListCollection.get_Item(String strListName) 在 MyWebPart.WebPartTest.RenderWebPart(HtmlTextWriter output)
forum2621 2009-12-01
  • 打赏
  • 举报
回复
我把错误输出了:
System.IO.FileNotFoundException: 系统找不到指定的文件。 (异常来自 HRESULT:0x80070002) 在 Microsoft.SharePoint.Library.SPRequestInternalClass.GetMetadataForUrl(String bstrUrl, Int32 METADATAFLAGS, Guid& pgListId, Int32& plItemId, Int32& plType, Object& pvarFileOrFolder) 在 Microsoft.SharePoint.Library.SPRequest.GetMetadataForUrl(String bstrUrl, Int32 METADATAFLAGS, Guid& pgListId, Int32& plItemId, Int32& plType, Object& pvarFileOrFolder) 在 Microsoft.SharePoint.SPWeb.GetMetadataForUrl(String relUrl, Int32 mondoProcHint, Guid& listId, Int32& itemId, Int32& typeOfObject, Object& fileOrFolder) 在 Microsoft.SharePoint.SPWeb.GetList(String strUrl) 在 MyWebPart.WebPartTest.RenderWebPart(HtmlTextWriter output)

判断list存在不存在用什么方法,list != null报错啊。

3,245

社区成员

发帖
与我相关
我的任务
社区描述
企业开发 SharePoint
社区管理员
  • SharePoint社区
  • 霖雨 - LinyuLoveTJ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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