2014-05-21 32 views
0

我在我的asp.net項目中有一個表格,我在運行時動態地將一些按鈕添加到某些單元格。我基於從數據庫中獲取的數據添加這些按鈕。用戶從下拉列表中選擇一個項目,然後單擊顯示計劃按鈕並填寫表格。下面是某一條我的代碼:如何在asp.net中刷新表格UI的數據變化?

for (int i = startindex; i < endindex; i++) 
{ 


    Button b = new Button(); 
    b.Text = subj + " " + numb + " " + section; 
    b.Attributes.Add("onclick", "popWin()"); 
    b.CssClass = "roundedbuttons"; 

    Color clr = getCourseColor(subj + numb + section, courses); 
    b.BackColor = Color.Aqua; 
    b.Enabled = true; 
    b.EnableViewState = true; 
    b.UseSubmitBehavior = false;  
    b.UseSubmitBehavior = false; 
    b.Click += new EventHandler(btn_Click); 
    string cellPosition = (i).ToString() + ";" + (dayindex).ToString(); 
    b.CommandArgument = cellPosition; 
    table_row_click = true;       

    table_filter_instructor_schedule.Rows[i].Cells[dayindex].Controls.Add(b); 

    pageControls.Add(new PageControls(i, dayindex, null, subj + " " + numb + " " + section, "button")); 

    visualTable = table_filter_instructor_schedule; 

    } 

這裏是表的樣子:

​​

然後,在某些時候,我更新數據庫,並更改了一些數據,當用戶按下保存更改按鈕:

string selectedRooom = dropdown_available_rooms.SelectedValue.ToString(); 
     var values = selectedRooom.Split(' '); 
     string newbldg = values[0], newroom = values[1]; 
     btnPopUp_ModalPopupExtender.Hide(); 
     //Update the database, and put the course to its new hours 

     //term şimdilik 
     SqlCommand commnd = new SqlCommand("UPDATE Timetable SET [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected] AND [email protected] AND [email protected] AND term='201101' AND [email protected] AND [email protected]", CommonFunctions.con); 
     commnd.Parameters.AddWithValue("@newstarthour", selected_new_starthour); 
     commnd.Parameters.AddWithValue("@newendhour", selected_new_endhour); 
     commnd.Parameters.AddWithValue("@newday", selected_new_day); 
     commnd.Parameters.AddWithValue("@newbuilding", newbldg); 
     commnd.Parameters.AddWithValue("@newroom", newroom); 
     commnd.Parameters.AddWithValue("@subj", clicked_course_code); 
     commnd.Parameters.AddWithValue("@numb", clicked_course_number); 
     commnd.Parameters.AddWithValue("@section", clicked_course_section); 
     commnd.Parameters.AddWithValue("@starthour", clicked_course_starthour); 
     commnd.Parameters.AddWithValue("@endhour", clicked_course_endhour); 
     commnd.ExecuteNonQuery(); 

一切工作正常,直到那一點。問題是,這些變化並沒有反映在可視化表格中,有些按鈕的位置(單元格)必須在表格中進行更改。如果我轉到另一個頁面,然後重新打開具有此可視表格的頁面,表格會更新並正常工作。但是,在用戶單擊保存更改按鈕後,更改不會立即在表格中看到。即使CausesValidation屬性設置爲true,保存更改按鈕也不會導致回發。我不知何故需要重新加載頁面。我試過:

Response.Redirect(Request.RawUrl); 

數據更新後,但這次頁面重新加載,自然表中顯示爲空。

那麼,誰能幫助我呢?

謝謝。

+1

每次更新後都必須重新調整表格。 – Fals

回答

0

這簡直是什麼,我會建議的基礎上,我認爲你正在嘗試做的:

當您單擊「顯示進度」的運行應該放到它自己的子代碼。 當用戶點擊「顯示時間表」時調用該子。 然後在代碼後再次調用Sub以「保存更改」。

至於此評論:

保存更改按鈕不會導致即使 CausesValidation屬性設置爲true回發。

頁面上是否有更新面板?這可能會導致回發問題。

+0

感謝您的回答,是的,我的網頁中有一個模式彈出式擴展程序,並且保存按鈕位於該彈出窗口中。它的onclick函數被調用並且可以工作,但是不會發生回傳 – yrazlik

+0

所以有一個彈出式擴展器,但是有更新面板嗎?聽起來好像有。彈出窗口中還有表格還是保存按鈕? –

0

這是我給你的問題。你是否動態創建這些按鈕?如果是這樣,他們將永遠不會開火。這是爲什麼。如果在表單渲染時沒有創建按鈕,或者當類(代碼隱藏文件)被實例化時,這些按鈕不會與火災或點擊事件相關聯。首先需要在應用程序啓動時創建按鈕,或者在Page_Load事件觸發時更直接。無論您創建了多少個,只要在應用程序運行時有足夠的空間可用即可。因此,從一組按鈕開始,並使用一個循環來創建儘可能多的,你想或需要。執行此操作並添加諸如單擊事件名稱之類的屬性。我在下面發佈一些代碼,會告訴你如何做到這一點。如果您對我在這裏發佈的任何內容有任何疑問,請提問。

/* Place this above the Page_Load event */ 
    Button[] btnArray; 

    /* Call this method or something similar to instantiate the buttons */ 
    private void InstantiateBtns() 
    { 
     //Instatiate every possible button needed for some purpose 
     btnArray = new Button[1000]; 

     for (int i = 0; i < btnArray.Length; i++) 
     { 
      btnArray[i] = new Button(); 

      // sets the attributes for the button control 
      btnArray[i].ID = "ContactBtn" + i; 
      btnArray[i].Width = 295; 
      btnArray[i].Height = 50; 
      btnArray[i].BorderStyle = BorderStyle.None; 
      btnArray[i].CssClass = "contacts"; 
      btnArray[i].Click += new EventHandler(this.btnContactBtn_Click); 
      btnArray[i].Attributes.Add("runat", "server"); 
     } 

    } // end of InstantiateBtns() 
+0

謝謝,但是,我在回發後重新創建按鈕。我的代碼中的pageControls變量保持動態創建的按鈕,並且這些按鈕的onclick函數可以正常工作。問題是,我有一個模式彈出和保存按鈕的這種模式彈出窗口也被觸發,但當保存按鈕被點擊時,沒有回傳,Page_Load不叫 – yrazlik

+0

好吧,你是否創建保存按鈕,當頁面是第一次通過在Page_Load事件中創建它或通過Page_Load事件調用方法加載? –

+0

您在「b.Attributes.Add(」onclick「,」popWin()「)之上放置的語句;」你試圖開火的事件?如果是這樣嘗試我在我的帖子「btnArray [i] .Click + = new EventHandler(this.btnContactBtn_Click)中顯示;」?我知道這是添加點擊事件的方法是正確的,因爲我經常使用它。 –