2012-07-09 37 views
2

我在UpdatePanel中有一個佔位符控件,並且當單擊添加車輛按鈕時,我需要一個新的「行」控件出現(第一個「行」是添加聲明)。用戶控件然而被添加但不顯示。我怎樣才能解決這個問題?用戶控件在回發上添加,但不顯示

protected void AddVehicleButton_Click(object sender, EventArgs e) 
     { 

      int count = Convert.ToInt32(VehicleRegistrationCountHiddenField.Value); 
      var TBId = "VehicleRegistrationEnhancedTextBox" + count; 
      IList<Panel> oldPanels = (IList<Panel>)Session["VehiclePanels"] ?? new List<Panel>(); 

      //Seperator 
      Literal hr = new Literal { Text = "<HR/>" }; 

      //Vehicle Registration 
      UserControl uc = new UserControl(){ID="3"}; 
      uc.LoadControl("~/Controls/ImageUploadAndCrop/ImageUploadAndCrop.ascx"); 


      Label vehicleRegistration = new Label 
              { 
               ID = TBId + "_Label", 
               AssociatedControlID = TBId, 
               Text = "Vehicle Registration:" 
              }; 
      EnhancedTextBox vehicleTypeTextBox = new EnhancedTextBox 
                { 
                 ID = TBId, 
                 Required = true, 
                 RequiredErrorText = "Vehicle Registration is a required field." 
                }; 

      //Readd previously added panels 
      foreach (var addedPanel in oldPanels) 
      { 
       AddVehiclePlaceholder.Controls.Add(addedPanel); 
      } 

      //Add new controls to the form 
      Panel newPanel = new Panel(); 

      newPanel.Controls.Add(hr); 
      newPanel.Controls.Add(uc); 
      newPanel.Controls.Add(vehicleRegistration); 
      newPanel.Controls.Add(vehicleTypeTextBox); 

      AddVehiclePlaceholder.Controls.Add(newPanel); 

      //Increment the ID count 
      count++; 
      VehicleRegistrationCountHiddenField.Value = count.ToString(); 

      //Save the panel to the Session. 
      oldPanels.Add(newPanel); 
      Session["VehiclePanels"] = oldPanels;   

     } 

的HTML低於:

<div id="Step2" style="" data-step="2"> 
<h2> Step 2 (optional): Capture the offender(s) vehicle Information. </h2> 
<hr> 
<div id="VehicleTypeFields"> 
<div> 
<label for="">Vehicle Registration</label> 
<div id="Body_Body_UpdatePanel1"> 
<div> 
<div> 
<script type="text/javascript" src="/Controls/ImageUploadAndCrop/Javascript/jquery.Jcrop.min.js"> 
<link type="text/css" rel="stylesheet" href="/Controls/ImageUploadAndCrop/CSS/jquery.Jcrop.css"> 
<script type="text/javascript"> 
<div> 
<div id="Body_Body_VehicleRegistrationImageUploadAndCrop_UploadPanel"> 
</div> 
<input id="Body_Body_VehicleRegistrationEnhancedTextBox" type="text" placeholder="CA123-456" name="ctl00$ctl00$Body$Body$VehicleRegistrationEnhancedTextBox"> 
</div> 
</div> 
</div> 
</div> 
<div id="Body_Body_AddVehiclesUpdatePanel"> 
<input id="Body_Body_VehicleRegistrationCountHiddenField" type="hidden" value="2" name="ctl00$ctl00$Body$Body$VehicleRegistrationCountHiddenField"> 
<div> 
<hr> 
<label id="Body_Body_VehicleRegistrationEnhancedTextBox1_Label" for="Body_Body_VehicleRegistrationEnhancedTextBox1">Vehicle Registration:</label> 
<input id="Body_Body_VehicleRegistrationEnhancedTextBox1" type="text" name="ctl00$ctl00$Body$Body$VehicleRegistrationEnhancedTextBox1"> 
<span id="Body_Body_VehicleRegistrationEnhancedTextBox1_RequiredFieldValidator" style="display:none;">Vehicle Registration is a required field.</span> 
</div> 
</div> 
</div> 
+0

你能告訴我們HTML – Adil 2012-07-09 11:30:17

回答

1

的問題是你對用戶控件的新實例,而不是頁面的執行LoadControl使用。 IE瀏覽器,而不是這樣的:

UserControl uc = new UserControl(){ID="3"}; 
uc.LoadControl("~/Controls/ImageUploadAndCrop/ImageUploadAndCrop.ascx"); 

而是執行此操作:

Control uc = LoadControl("~/Controls/ImageUploadAndCrop/ImageUploadAndCrop.ascx"); 
uc.ID = 3; 

我認爲這將解決您的,沒有任何顯示,但仍有其他問題迫在眉睫的問題。儘管可能會將整個控件存儲在會話狀態中,但我自己的測試顯示這樣做存在異常。我會確保給每個用戶控件的實例一個唯一的ID(每次不是「3」),並將這些ID存儲在某個地方。然後循環通過這些ID,爲每一個調用LoadControl,在循環時設置控件ID(爲了將過去的視圖狀態重新應用於用戶控件,這是必需的)

我也將該循環移至Page_Init或Page_Load,這樣你的動態創建的用戶控件就可以正確地參與到頁面的生命週期中,在那個點擊事件中創建它們對於他們捕捉他們自己的事件來說已經太晚了,並且在回調之外的情況下它們將不會被創建那點擊事件

相關問題