嗨, 请原谅,因为这不是纯粹的sharepoint或Asp.NET应用程序,所以我发布了Sharepoint Overflow以及Stack Overflow。 谢谢。
我开发了一个Webpart并部署在SP 2007 Site中。 我在gridview中显示数据,我启用了分页,它可以显示10条记录。但是,当我单击下一个页码时,会出现错误“发生意外错误。解决Windows SharePoint Services问题”任何人都可以告诉我这里错过了什么。
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace EVentHandlerWebPartGridView
{
[Guid("d75310a6-2db3-431c-a45d-351e4ba0bda1")]
public class EventHandlerWebPartGridView : System.Web.UI.WebControls.WebParts.WebPart
{
// Constructors for the web Part
public EventHandlerWebPartGridView()
{
}
// Member variable for the class
private string _cnString = string.Empty;
// control definitions
protected Label lblError;
protected Button btnExecuteSQL;
protected TextBox txtSql;
protected GridView gvResults;
// WebPart Parameter for getting the Connection string
[
WebBrowsable(true),
WebDisplayName("Connection String"),
Personalizable(PersonalizationScope.User)
]
public string CNString
{
get { return _cnString; }
set { _cnString = value; }
}
// Override Method- place to initialize the controls
protected override void CreateChildControls()
{
//label initialization and Properties
lblError = new Label();
this.Controls.Add(lblError);
// textbox control and Propetrties
txtSql = new TextBox();
txtSql.Width = Unit.Pixel(400);
txtSql.Height = Unit.Pixel(200);
txtSql.TextMode = TextBoxMode.MultiLine;
this.Controls.Add(txtSql);
//button Initialization and Properties
btnExecuteSQL = new Button();
btnExecuteSQL.Text = "Execute SQL";
btnExecuteSQL.ToolTip = "Click On this to see the Reults of the SQL Query";
btnExecuteSQL.Click += new EventHandler(btnExecuteSQL_Click);
this.Controls.Add(btnExecuteSQL);
//gridview Initialization
gvResults = new GridView();
gvResults.Width = Unit.Percentage(100);
gvResults.AlternatingRowStyle.BackColor = System.Drawing.Color.Blue;
gvResults.AllowPaging = true;
gvResults.PageSize = 10;
Controls.Add(gvResults);
}
//Button Click Event handler
protected void btnExecuteSQL_Click(object sender, EventArgs e)
{
// if the connection string is not empty
if (_cnString.Trim() != string.Empty)
{
try
{
SqlConnection con = new SqlConnection(_cnString);
SqlCommand cmd = new SqlCommand(txtSql.Text, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvResults.DataSource = ds.Tables[0].DefaultView;
gvResults.DataBind();
}
// Any Errors in the above Code
catch (Exception ex)
{
gvResults.DataSource = null;
gvResults.DataBind();
lblError.Text = ex.Message;
lblError.Visible = true;
}
}
// If the Connection string is Empty
else
{
gvResults.DataSource = null;
gvResults.DataBind();
lblError.Text = "Please Enter a Connection string";
lblError.Visible = true;
}
}
// Rendering the Data nows
protected override void Render(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Table); //table Starts
writer.RenderBeginTag(HtmlTextWriterTag.Tr); // Tr Starts
writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");// Adds an attribute tag
writer.RenderBeginTag(HtmlTextWriterTag.Td); // Td tag starts Here
lblError.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag(); //tr nd
writer.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
writer.RenderBeginTag(HtmlTextWriterTag.Tr); // tr
writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
writer.Write("Enter a SQL Statement");
writer.RenderEndTag();// end td
// Now Display the Text Box
writer.RenderBeginTag(HtmlTextWriterTag.Td); // Td start Tag
txtSql.RenderControl(writer);
writer.RenderEndTag(); // td End Tag
writer.RenderEndTag();//tr End
// Now Display the Button
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
btnExecuteSQL.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
//Now Display the GridView
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
gvResults.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag(); //Table End
}
}
}
答案 0 :(得分:0)
在web.config文件中,将CustomErrors =“On”更改为CustomErrors =“Off”。这将显示实际的错误消息。您可能没有处理分页事件。