2010-12-14 37 views
2

我將首先告訴我,我不是ASC/C#開發人員,這是爲了作業。 (我希望有人會明白我想要的:d,因爲我不知道到底條款)Asp.net從控件檢索內容

我有Friends.aspx和Friends.aspx.cs

在Friends.aspx我有類似
<%@ Reference Control="~/FriendBox.ascx" %>
<div class="controlcontainer clearfix friendlist_containeritems" id="divFriends" runat="server"> </div>

在Friends.aspx.cs我有這個填充divFriends:


foreach (FBUser user in list){ 
     Control control = LoadControl("FriendBox.ascx"); 
     FriendBox myControl = (FriendBox)control; 
     myControl.user = user;     
     divFriends.Controls.Add(myControl);     
    } 

在我的頁面有一個表格,並divFriends是表單內。我有一個這種形式的按鈕。如果我只是按下按鈕,我的頁面被提交,我可以從選定的複選框中檢索值。 (每個控件包含一個複選框)

Everithing工作正常,直到我試圖通過Ajax(JQuery)提交頁面。 我的問題是,即使複選框在頁面上(我可以選擇它們,選擇它們),當我做ajax提交我無法訪問複選框的值。 這是我使用從複選框檢索值的代碼:

 
foreach(Control ctrl in divFriends.Controls) {
var friendBox = ctrl as FriendBox; //i can get here
if (friendBox != null) { //i never get here - because friendBox is allways null } }

How can i make the content from divFriends accesible when i submit my form with Jquery? Thanks in advance.

Edit: here is the javascript i use


$(document).ready(function() { 
     $('.big').click(function() { 
      //.big is my button 
      $('.big').attr('disabled', true);

 var form_form = $(document).find('form'); 
     $.ajax({ 
      type: 'POST', 
      url: form_form.attr('action'), 
      data: form_form.serialize(), 
      dataType: 'json', 
      success: function (data) {      
       console.log('success'); 
      }, 
      complete: function (XMLHttpRequest, textStatus) { 
       $('.big').attr('disabled', false); 
      } 
     }); 
     return false; 
    }); 

});


JavaScript的工作,因爲我可以提交數據,並且可以接收數據返回(JSON),當我看到submited數據我沒有divFriends的複選框。

+0

你可以顯示提交表單的JavaScript嗎? – Josh 2010-12-14 21:06:07

+0

這是一個非常複雜的家庭作業。 :/ – Greg 2010-12-14 21:06:36

+2

在你的代碼隱藏的地方是添加控件的foreach循環嗎?哪個事件,它周圍的邏輯?也有什麼特別的原因,你正在動態地添加控件,而不是使用中繼器/其他數據綁定控件來聲明(拼寫?)? – RichardW1001 2010-12-14 21:09:53

回答

2

該問題似乎是asp.net狀態管理問題。你只是一次添加FriendBox(我假設,可能帶有if.page.ispostback類型的),而不是每頁擊中。由於FriendBox是動態添加的,因此它不會存活回發,因此當您嘗試閱讀時它不存在。

您應該在每次頁面加載時添加FriendBox,在加載viewstate之前的Init上。在init中添加控件將允許Viewstate跟蹤這些值,並可能會解決問題。