2011-12-15 145 views
1

我使用Visual Studio创建了一个Web部件,以在GridView中显示列表的选定列。但问题是,无论何时我将语言环境更改为英国 - 英国(这是默认英语 - 美国),不幸的是它和网站对它没有影响,尽管日期格式应该被更改。Sharepoint区域设置

我尝试下面的代码更改地区和日期格式:

protected void btnClick_Event(object sender, EventArgs e) 
    { 
     using (SPSite osite = new SPSite(SPContext.Current.Web.Url)) 
     { 
      using (SPWeb oweb = osite.OpenWeb()) 
      { 
       if (DropDownList1.SelectedValue == "English-UK") 
       { 
        oweb.AllowUnsafeUpdates = true; 
        oweb.Locale = new System.Globalization.CultureInfo("en-GB"); 
        oweb.Update(); 
       } 
       else if(DropDownList1.SelectedValue == "English-US") 
       { 
        oweb.Locale = new System.Globalization.CultureInfo("en-US"); 
        oweb.Update(); 
       } 

       lblmsg.Text = "Region changed successfully";   
      } 
     } 
    } 

的WebPart代码如下:

using System; 
using System.ComponentModel; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.WebControls; 
using System.Data; 

namespace SharePointProject100.CustomGridWebpart 
{ 
    [ToolboxItemAttribute(false)] 
    public class CustomGridWebpart : WebPart 
    { 

    SPGridView grdview = new SPGridView(); 
    protected override void CreateChildControls() 
    { 
     grdview.ID = "grdview"; 
     grdview.AutoGenerateColumns = false; 

     this.Controls.Add(grdview); 
     using (SPSite osite = new SPSite(SPContext.Current.Web.Url)) 
     { 
      using (SPWeb web = osite.OpenWeb()) 
      { 
       SPList mylist = web.Lists["EmployeeDetails"]; 
       BindToGrid(mylist, grdview); 
      } 
     } 
    } 

    private void BindToGrid(SPList myList, SPGridView gridView) 
    { 
     // get all the listitem 
     SPListItemCollection results = myList.Items; 

     // create the datatable object 
     DataTable table = new DataTable(); 
     table.Columns.Add("Employee Name", typeof(string)); 
     table.Columns.Add("DOB", typeof(string)); 
     table.Columns.Add("JoiningDate", typeof(string)); 
     table.Columns.Add("MembershipExpiryDate", typeof(string)); 

     // Create rows for each splistitem 
     DataRow row; 
     foreach (SPListItem result in results) 
     { 
      row = table.Rows.Add(); 
      row["Employee Name"] = Convert.ToString(result["Employee Name"]); 
      row["DOB"] = Convert.ToString(result["DOB"]); 
      row["JoiningDate"] = Convert.ToString(result["JoiningDate"]); 
      row["MembershipExpiryDate"] = Convert.ToString(result["MembershipExpiryDate"]); 
     } 

     // create the bound fields 
     SPBoundField boundField; 
     boundField = new SPBoundField(); 
     boundField.HeaderText = "Employee Name"; 
     boundField.DataField = "Employee Name"; 
     boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
     boundField.ItemStyle.Wrap = false; 
     gridView.Columns.Add(boundField); 

     boundField = new SPBoundField(); 
     boundField.HeaderText = "DOB"; 
     boundField.DataField = "DOB"; 
     boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
     boundField.ItemStyle.Wrap = false; 
     gridView.Columns.Add(boundField); 

     boundField = new SPBoundField(); 
     boundField.HeaderText = "JoiningDate"; 
     boundField.DataField = "JoiningDate"; 
     boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
     boundField.ItemStyle.Wrap = false; 
     gridView.Columns.Add(boundField); 

     boundField = new SPBoundField(); 
     boundField.HeaderText = "MembershipExpiryDate"; 
     boundField.DataField = "MembershipExpiryDate"; 
     boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
     boundField.ItemStyle.Wrap = false; 
     gridView.Columns.Add(boundField); 

     gridView.AutoGenerateColumns = false; 
     gridView.DataSource = table.DefaultView; 
     gridView.DataBind(); 
    } 
} 

}

+0

要更改区域设置,请按照以下步骤操作:站点操作 - >站点设置 - >站点管理 - >区域设置 – 2011-12-15 11:11:27

回答

1

设置的LocaleID在web的RegionalSettings是应该将其标记为“脏”(查看ILSpy中的反汇编代码),但是我没有任何运气可以让它基于这种改变来“更新”其他程序。