2010-04-21 29 views
6

我一直在嘗試通過使用Javascript設置隱藏輸入的值,然後從我的C#代碼隱藏中訪問值。當我運行下面複製的代碼時,分配給assignedID的值爲「」,我假設它是隱藏輸入的默認值。如果我手動設置了html標籤中的值,那麼分配的ID被設置爲該值。在Javascript中設置隱藏的輸入值,然後在代碼隱藏中訪問它

此行爲向我建議,onClientClick和onClick事件觸發之間的輸入值正在重置(重新呈現?)。

我將不勝感激任何幫助。我花了幾個小時試圖解決看起來像一個非常簡單的問題。

HTML/JavaScript的:

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
<title>Admin Page - Manage Tasks</title> 
    <script language="javascript" type="text/javascript"> 
     function PopulateAssignedIDHiddenInput() { 
      var source = document.getElementById('assignedLinguistListBox'); 
      var s = ""; 
      var count = source.length; 
      for (var i = count - 1; i >= 0; i--) { 
       var item = source.options[i]; 
       if (s == "") { s = source.options[i].value; } 
       else { s = s.concat(",",source.options[i].value); } 
      } 
      document.getElementById('assignedIDHiddenInput').Value = s; 
      // I have confirmed that, at this point, the value of 
      // the hidden input is set properly 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Panel id="EditMode" runat="server"> 
      <table style="border: none;"> 
       <tr> 
        <td> 
         <asp:Label ID="availableLinguistLabel" runat="server" Text="Available"></asp:Label><br /> 
         <asp:ListBox ID="availableLinguistListBox" runat="server" Rows="10" SelectionMode="Multiple"></asp:ListBox> 
        </td> 
        <td> 
         <input type="button" name="right" value="&gt;&gt;" 
          onclick="Javascript:MoveItem('availableLinguistListBox', 'assignedLinguistListBox');" /><br /><br /> 
         <input type="button" name="left" value="&lt;&lt;" 
          onclick="Javascript:MoveItem('assignedLinguistListBox', 'availableLinguistListBox');" /> 
        </td> 
        <td> 
         <asp:Label ID="assignedLinguistLabel" runat="server" Text="Assigned To"></asp:Label><br /> 
         <asp:ListBox ID="assignedLinguistListBox" runat="server" Rows="10" SelectionMode="Multiple"></asp:ListBox> 
        </td> 
       </tr> 
      </table> 
      //-snip- 
      <asp:Button ID="save_task_changes_button" runat="server" ToolTip="Click to save changes to task" 
       Text="Save Changes" OnClick="save_task_changes_button_click" OnClientClick="Javascript:PopulateAssignedIDHiddenInput()" /> 
     </asp:Panel> 

     <!-- Hidden Inputs --> 
     <!-- Note that I have also tried setting runat="server" with no change --> 
     <input id="assignedIDHiddenInput" name="assignedIDHiddenInput" type="hidden" /> 
    </div> 
    </form> 
</body> 

C#

protected void save_task_changes_button_click(object sender, EventArgs e) 
{ 
    string assignedIDs = Request.Form["assignedIDHiddenInput"]; 
    // Here, assignedIDs == ""; also, Request.Params["assignedIDHiddenInput"] == "" 
    // -snip- 
} 

回答

7

在javascript中你需要的value屬性爲小寫,這樣的:

document.getElementById('assignedIDHiddenInput').value = s; 

釷恩將正確設置:) You can see an example in action here

但如果你提醒.Value它會顯示你的價值,你實際上已經增加了一個新的.Value屬性,但沒有設置輸入的.value屬性是什麼發佈到服務器。上面的示例鏈接說明了這兩種方式。

您也可以通過使用數組,而不是字符串連接,這樣讓它快一點,特別是如果你有很多的選擇:

var source = document.getElementById('assignedLinguistListBox'); 
var opts = []; 
for (var i = 0; i < source.options.length; i++) { 
    opts.push(source.options[i].value); 
} 
var s = opts.join(','); 

編輯:上面的代碼更新,CMS是前面的方法依賴於瀏覽器,上面現在應該表現得一貫。此外,如果jQuery是一種選擇,也有得到這個信息的是,像這樣的短方式:

var s = $('#assignedLinguistListBox option').map(function() { 
      return this.value; 
     }).get().join(','); 
$('#assignedIDHiddenInput').val(s); 

You can see a working example of this here

+0

哦......哇。我無法相信!這樣一個愚蠢而簡單的錯誤。謝謝你,尼克! – Siegesmith 2010-04-21 22:44:57

+0

使用'for ... in'語句對'options' ['HTMLOptionsCollection'](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#HTMLOptionsCollection)對象進行迭代給你真正意想不到的結果,在一些瀏覽器中,即使數字選項索引不會被迭代,我會推薦一個普通的'for'循環,檢查[這個例子](http://jsbin.com/ohebe/)。 – CMS 2010-04-21 23:46:26

+0

@CMS - 瀏覽器依賴關係上的重點,也習慣於這裏的每個關閉。我更新了上面的代碼,以防其他人在稍後遇到此問題需要相同的信息。 – 2010-04-22 00:47:21

1

我假設在這裏ASP.NET。

如果是這樣,你的問題是由ASP.NET生成的HTML控件的id不會是你在腳本中引用的「assignedIDHiddenInput」。 ASP.NET在聲明性地從HTML頁面中指定HTML之前更改HTML。在頁面上查看源代碼,您將看到我的意思。

這裏是圍繞一個辦法:

document.getElementById('<%=assignedIDHiddenInput.ClientID %>').value = s; 

更新:正如評論指出,如果控制設置爲RUNAT =服務器這僅僅是相關的。

+0

這不會是一個問題,他的輸入不是'runat =「server」'。 – 2010-04-21 22:40:55

+0

我一直以爲你只有分配了一個clientid,如果它是「runat = server」? – 2010-04-21 22:41:23

+0

好點。他提到「<! - 請注意,我也嘗試過設置runat =」server「而沒有任何更改 - >」所以我認爲他可能想要以這種方式運行它,但他的服務器端代碼使用「 Request.Form [「assignedIDHiddenInput」],所以這可能是一個爭論點 – JohnFx 2010-04-21 22:44:46

0

我認爲ASP.NET調用JavaScript來執行該控件的回發之前您的JavaScript函數被調用來填充該隱藏值。

我認爲可以禁用默認回發並自己處理它,但我相信其他人可以提供更好的建議。

在你的函數中粘貼一個alert()函數,看它是否真的在觸發post-back之前被調用。

相關問題