I have a web part with a simple asp.net/c# form. The first thing I noticed is that the form controls are not keeping their values after a postback. I thought this seemed weird since my web.config has viewstate enabled. I then tried to manually create viewstate objects to hold certain pieces of information, but this is not working either. I then enabled trace on the page, and cannot see anything about viewstate anywhere. Can someone help me with this? Does sharepoint have this turned off somewhere? Thanks.
namespace PortalAdmin.AdminTool
{
public partial class AdminToolUserControl : UserControl
{
SPWeb thisWeb = SPContext.Current.Web;
protected void Page_Load(object sender, EventArgs e)
{
thisWeb.AllowUnsafeUpdates = true;
if (!Page.IsPostBack)
{
lblMessage.Text = "";
}
if (Page.IsPostBack)
{
if (ViewState["reportPath"] != null)
txtReportName.Text = ViewState["reportPath"].ToString();
if (ViewState["action"] != null)
{
switch (ViewState["action"].ToString())
{
case "preview":
lblMessage.Text = "Previewing report...";
break;
case "clear":
lblMessage.Text = "Report cleared...";
break;
case "complete":
lblMessage.Text = "Report added...";
break;
}
}
}
}
protected void btnAddWebPart_Click(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Site.OpenWeb("/dev/RE/West/SA/3050");
web.AllowUnsafeUpdates = true;
SPFile f = web.GetFile("SitePages/Page.aspx");
SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);
OBIEEReport report = new OBIEEReport();
report.ReportPath = Request.QueryString["path"].ToString();
report.Title = "REGIS Report";
report.ChromeType = PartChromeType.None;
wpm.AddWebPart(report, "Zone 1", 1);
wpm.Dispose();
web.Update();
ViewState["action"] = "complete";
Response.Redirect(Request.Url.GetLeftPart(UriPartial.Path));
}
protected void btnClearWebPart_Click(object sender, EventArgs e)
{
txtReportName.Text = "";
SPFile f = thisWeb.GetFile(HttpContext.Current.Request.Url.ToString());
SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);
for (int i = 0; i < wpm.WebParts.Count; i++)
{
if (wpm.WebParts[i].Title != "Admin Tool")
wpm.DeleteWebPart(wpm.WebParts[i]);
}
wpm.Dispose();
thisWeb.Update();
ViewState["action"] = "clear";
Response.Redirect(Request.Url.GetLeftPart(UriPartial.Path));
}
protected void btnPreviewWebPart_Click(object sender, EventArgs e)
{
SPFile f = thisWeb.GetFile(HttpContext.Current.Request.Url.ToString());
SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);
OBIEEReport report = new OBIEEReport();
report.ReportPath = txtReportName.Text;
report.Title = "REGIS Report";
report.ChromeType = PartChromeType.None;
if (wpm.WebParts.Count == 2)
{
lblMessage.Text = "You are already previewing a report";
}
else
{
wpm.AddWebPart(report, "Zone 1", 1);
wpm.Dispose();
thisWeb.Update();
ViewState["action"] = "preview";
ViewState["reportPath"] = txtReportName.Text;
Response.Redirect(Request.Url.GetLeftPart(UriPartial.Path));
}
}
}
}