2013-07-23 67 views
0

我在Web應用程序中有兩個站點。我想要做的是使用按鈕從站點B中的Web部件更新站點A的列表。GridView回發錯誤

問題是,如果我不使用Page.IsPostBack,當單擊該按鈕時,頁面會拋出未處理的異常,但是會更新站點A中的列表。但是如果我使用Page.IsPostBack,我沒有得到例外,但這個過程並沒有通過;事實上,當按鈕被點擊時,它甚至不會進入事件處理程序。

if (!Page.IsPostBack) 
      { 
       using (SPSite site = new SPSite(SPContext.Current.Web.Url)) 
       { 
        SPQuery query = new SPQuery(); 
        query.Query = @"Query><OrderBy><FieldRef Name='Title' /></OrderBy><Query>"; 
        query.ViewAttributes = "<FieldRef Name='Title' />"; 
        query.ViewFields = string.Concat(
         "<FieldRef Name='Title' />", 
         "<FieldRef Name='Status' />", 
         "<FieldRef Name='Severity' />", 
         "<FieldRef Name='Comment' />"); 

        query.ViewFieldsOnly = true; 
        showGrid = new GridView(); 

        this.Controls.Add(showGrid); 

        SPList list = SPContext.Current.Site.RootWeb.Lists["Ticket List"]; 
        DataTable dt = new DataTable();      

        ButtonField approveBtn = new ButtonField(); 
        approveBtn.ButtonType = ButtonType.Button; 
        approveBtn.CommandName = "Update"; 


        approveBtn.Text = "Approve"; 
        showGrid.Columns.Add(approveBtn); 

        dt = list.Items.GetDataTable().Copy(); 
        dt = list.GetItems(query).GetDataTable(); 
        showGrid.DataSource = dt; 

        showGrid.DataBind(); 

        showGrid.RowCommand += new GridViewCommandEventHandler(inventoryGridView_RowCommand); 
       } 
      } 

事件處理

if (e.CommandName == "Update") 
      { 
       int index = Convert.ToInt32(e.CommandArgument); 
       GridViewRow row = showGrid.Rows[index]; 
       using (SPSite site = new SPSite(SPContext.Current.Web.Url)) 
       { 
        using (SPWeb Web = site.OpenWeb()) 
        { 
         Web.AllowUnsafeUpdates = true; 
         // Open List 
         SPList list = SPContext.Current.Site.RootWeb.Lists["Ticket List"]; 
         SPListItem _listItem = list.Items[row.RowIndex]; 
         _listItem["Status"] = "Approved"; 
         _listItem.Update(); 
         Web.AllowUnsafeUpdates = false; 
        } 
       } 
      } 

回答

0

在頁面加載事件正在創建的網格,並把它添加到Controls集合。所以當你不打算回發....它會再次創建控件,但是當你嘗試添加它時會拋出異常。

通過調試解決方案進行檢查。

+0

我不確定我是否正確理解了你。你能否詳細說明一下?感謝您的迴應! – Nath

+0

你有調試你的解決方案嗎?你在哪裏爲網格分配了ID。 – Pushpendra

+0

這一切都完成在OnLoad塊 – Nath