2011-03-22 46 views
0

我是新的SharePoint的開發人員,並試圖創建搜索webpart窗口sharepoint服務3.0 [免費版],到目前爲止,我可以從列表框中選擇多個項目,但是當我試圖顯示選定的項目到文本框它迷路了。選擇的項目在回發丟失[Sharepoint wss3.0 webpart]

我會在這個問題上appriciate任何一個迴應,並可以指出我的錯誤,因爲這個問題有點迫切任何人都可以幫助我請。

感謝

下面是我的代碼對你的專家審查並請讓我知道我錯了。

using System; 
using System.Runtime.InteropServices; 
using System.Data; 
using System.Collections.ObjectModel; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Text; 
using System.Web; 
using System.Drawing; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Utilities; 
using Microsoft.SharePoint.WebControls; 
using Microsoft.SharePoint.WebPartPages; 
using System.Xml.Serialization; 
using System.Collections; 


namespace Filter_WebPart 
{ 
    [Guid("6641a7a3-d2c4-4fda-9ef5-89596845bd6e")] 

    public class Filter_WebPart : System.Web.UI.WebControls.WebParts.WebPart 
    { 
     protected DataSet _dataset;   
     protected ListBox lstRegion; 
     protected ListBox lstMaterial;   
     protected HtmlButton btnSubmit; 
     protected HtmlInputText txtDisplay;   

     private string myExceptions = "";     
     //private int[] Index1; 


     protected override void CreateChildControls() 
     {             
      try 
      {                 
       //Region Table/DataSet 
       lstRegion = new ListBox();         

       lstRegion.ID="lstRegion";     
       lstRegion.SelectionMode = ListSelectionMode.Multiple;         
       //lstRegion.EnableViewState = true;         
       //lstRegion.SelectedIndex = 0;         


       //Material Table/DataSet 
       lstMaterial = new ListBox();     
       lstMaterial.SelectionMode = ListSelectionMode.Multiple;     
       lstMaterial.EnableViewState = true; 

       btnSubmit = new HtmlButton();     
       btnSubmit.InnerText = "Filter";             
       btnSubmit.ServerClick += new EventHandler(btnSubmit_Click); 

       txtDisplay = new HtmlInputText();         


       //CommandButton 

       this.Controls.Add(lstRegion); 
       this.Controls.Add(lstMaterial); 
       this.Controls.Add(btnSubmit); 
       this.Controls.Add(txtDisplay);    

      } 
      catch(Exception ChildControlException) 
      { 
       myExceptions += "ChildControlException:" + ChildControlException.Message; 
      } 
      finally 
      { 
       //base.CreateChildControls(); 
      } 

     }   

     protected override void OnPreRender(EventArgs e) 
     {    
      if(!this.Page.IsPostBack) 
      {     
       lstMaterial.DataSource = GetMaterialList(); 
       lstMaterial.DataTextField = "Material Name"; 
       lstMaterial.DataValueField = "Material Name"; 
       lstMaterial.DataBind();                

       lstRegion.DataSource = GetRegionList();         
       lstRegion.DataTextField = "Region Name"; 
       lstRegion.DataValueField = "Region Name"; 
       lstRegion.DataBind(); 
       txtDisplay.Value="1 time"; 
      } 
      base.OnPreRender(e);    
     } 

     void btnSubmit_Click(object sender, EventArgs e) 
     {       
      string tmpStr="";         
      int k=0;  
      //int i=0; 


      lstMaterial.DataBind(); 
      lstRegion.DataBind(); 

      //int[] indx = lstRegion.GetSelectedIndices(); 
      //for(i=0;i<indx.Length;i++) 
      //{ 
      // tmpStr = tmpStr+","+lstRegion.Items[indx[i]].Text; 
      //} 


      //if(lstRegion.SelectedIndex >=0) 
      //{ 
       //for(i=0;i < lstRegion.Items.Count;i++) 
       //{ 
       // //if(i==5 || i==10) 
       // //{ 
       // // lstRegion.Items[i].Selected = true; 
       // //} 
       // if(lstRegion.Items[i].Selected) 
       // { 
       //  tmpStr = lstRegion.Items[i].Text;      
       // } 
       // k=k+1; 
       //} 
      //} 

      foreach(ListItem RgItem in lstRegion.Items) 
      {          
       if(RgItem.Selected == true) 
       {             
        tmpStr = tmpStr +","+RgItem.Text;      
        k=k+1; 
       }         
      }     

      //for(i=0;i<lstRegion.Items.Count;i++) 
      //{ 
      // if(lstRegion.Items[i].Selected == true) 
      // { 
      //  txtDisplay.Value = txtDisplay.Value +","+lstRegion.Items[i].Text;  
      //  k=k+1; 
      // }     
      //} 

      if(tmpStr != "") 
      {txtDisplay.Value = tmpStr;} 
      else{         
       txtDisplay.Value = k.ToString(); 
       btnSubmit.InnerText = "Done";} 


     }     


     protected override void OnInit(EventArgs e) 
     {        
      base.OnInit(e); 
      EnsureChildControls();              
     } 

     private DataSet GetRegionList() 
     {    
      _dataset = new DataSet(); 
      DataTable _tbl = new DataTable(); 
      DataColumn _tblcol = new DataColumn("Region Name"); 
      _tbl.Columns.Add(_tblcol);    

      SPWeb web = SPContext.Current.Site.RootWeb; 
      SPList myList = web.Lists["Service Area"]; 
      SPQuery query = new SPQuery(); 
      query.Query = ""; 
      SPListItemCollection items = myList.GetItems(query); 

       foreach (SPListItem item in items) 
       { 
        DataRow _row = _tbl.NewRow();      
        _row[0] = SPEncode.HtmlEncode(item["Region Name"].ToString()); 
        _tbl.Rows.Add(_row); 
       } 

      _dataset.Tables.Add(_tbl); 
      return _dataset; 
     } 

     private DataSet GetMaterialList() 
     { 
      _dataset = new DataSet(); 
      DataTable _tbl = new DataTable(); 
      DataColumn _tblcol = new DataColumn("Material Name"); 
      _tbl.Columns.Add(_tblcol); 
      SPWeb web = SPContext.Current.Site.RootWeb; 

       SPList myList = web.Lists["Material Master"]; 
       SPQuery query = new SPQuery(); 
       query.Query = ""; 
       SPListItemCollection items = myList.GetItems(query); 
       foreach (SPListItem item in items) 
       { 
        DataRow _row = _tbl.NewRow(); 
        _row[0] = SPEncode.HtmlEncode(item["Material Name"].ToString()); 
        _tbl.Rows.Add(_row); 
       } 

      _dataset.Tables.Add(_tbl); 
      return _dataset;    
     } 

     protected override void RenderContents(HtmlTextWriter output) 
     { 
      try 
      {     
       this.EnsureChildControls(); 
       lstRegion.RenderControl(output); 
       lstMaterial.RenderControl(output); 
       btnSubmit.RenderControl(output); 
       output.Write("<br>"); 
       txtDisplay.RenderControl(output);  

       //base.RenderContents(output); 
      } 
      catch (Exception RenderContentsException) 
      { 
       myExceptions += "RenderException:" + RenderContentsException.Message; 
      } 
      finally 
      { 
       if (myExceptions.Length > 0) 
       { 
        output.WriteLine(myExceptions); 
       } 
      } 
     } 


    } 
} 

回答

0

這應該是工作。我已經在我的2010農場上試過了你的代碼,它工作正常(應該也適用於WSS3)。

更新程序集後,您是否忘記執行iisreset?

找到解決方案的最佳機會是使用調試器。您可以將調試器附加到正確的w3wp.exe進程。如果您不知道哪一個,請全部選中。然後在你的事件處理程序中設置一個斷點,並檢查你的選擇丟失的位置。

你不需要重寫OnInit,你可以把EnsureChildControls()放在btnSubmit_Click中。