2011-08-09 58 views
2

這是問題所在。我有一個單選按鈕組(兩個單選按鈕)。ASP javascript radiobutton enable禁用不包括在回發ajax

這些傢伙最初被禁用。當用戶點擊一個複選框時,我通過設置rbtn.disabled = false;來動態啓用javascript中的單選按鈕,併爲其父(span元素)執行相同操作,以便它在IE中正確工作。 問題是這些動態啓用的單選按鈕在回發時不會返回(我在服務器端看到rbtn.Checked == false,而request.form不包含正確的值)。

這是怎麼發生的?除了帶隱藏字段的解決方法之外,是否還有其他修復程序?

預期的回答描述了回退策略,爲什麼/如何決定回發中包含哪些字段並解決此問題。

+0

嗨0xDEAD牛肉,只是爲了確保 - 您使用的ASP.net?還是經典的asp?因爲我不完全確定你的標籤。 –

回答

0

我還沒有機會自己測試這個,但我猜測(如果你使用ASP.net),你通過ASP.net服務器端代碼禁用單選按鈕,即通過服務器端控制。

然後使用javascript來重新使用它們?

我想也許服務器端仍然認爲他們被禁用 - 我不是100%爲什麼沒有挖掘futther(頁面生命週期中的某個地方)。

Perahaps快速解決方案將不是通過服務器端禁用單選按鈕,而是在頁面加載時在javascript中禁用它們?即在javscript中進行禁用和啓用。

1

submit之前,從單選按鈕刪除disabled屬性是這樣的:

document.getElementById("rbtnID").removeAttribute("disabled"); 

注意removeAttribute可在IE馬車,和IE還實現用於區分大小寫的第二屬性,參見MSDN文章。

還有removeAttributeNode這會刪除整個屬性節點,但它將node本身作爲參數而不是名稱。

var disabledNode = element.getAttributeNode('disabled'); 
element.removeAttributeNode(disabledNode); 

所以給這些鏡頭,讓我知道他們是如何發揮出來的!

0

在提交頁面之前,將收音機按鈕的disabled屬性設置爲false。在後面的代碼中使用Request.Form["radioButtonName"]讀取單選按鈕值。這會給你檢查的單選按鈕的值。

例子:

咱們說的單選按鈕列表名稱爲radioButton1有2個單選按鈕。當它在頁面上呈現時,單選按鈕將具有相同的名稱ctl0$radioButton1。無論如何,這取決於你的網頁的嵌套。你可以使用radioButton.UniqueID得到這個名字。

當頁面通過頁面上的任何操作提交時,執行下面的JavaScript將單選按鈕的禁用屬性設置爲false。

document.getElementById("radioButton1ItemCliendId").disabled = false; 

//If you want to check this radio button then 
document.getElementById("radioButton1ItemCliendId").checked = true; 

您所使用Request.Form[radioButton1.UniqueID]得到這個單選按鈕值在服務器端回發事件處理程序。它會給出檢查的單選按鈕值。

+0

這看起來非常類似於以前的答案! – Mrchief

+0

類似於什麼意義?你有沒有提到你必須使用'Request.From [radioButton.UniqueID]'獲取禁用的單選按鈕值後回? – ShankarSangoli

+0

解決方案基本相同(刪除禁用的屬性)。在服務器端讀取值--OP已經這樣做了(他在他的文章中提到過)。 – Mrchief

1

我知道下面的代碼並不整齊,但它完成了工作(如果我正確地理解了這個問題)。我在這裏複製/粘貼整個文件內容,讓你更容易玩。只需創建一個名爲WebForm1的Web表單並粘貼這些表單;

在.aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <script type="text/javascript"> 
    function enable(sender) { 
     if (sender.checked) { 
      document.getElementById('<%= RadioButton1.ClientID %>').removeAttribute('disabled'); 
      document.getElementById('<%= RadioButton2.ClientID %>').removeAttribute('disabled'); 
     } 
     else { 
      document.getElementById('<%= RadioButton1.ClientID %>').disabled = true; 
      document.getElementById('<%= RadioButton2.ClientID %>').disabled = true; 
     } 
    } 
    </script> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:CheckBox ID="CheckBox1" runat="server" onclick="enable(this)" /> 

    <asp:RadioButton ID="RadioButton1" runat="server" Text="1" 
     Enabled="false" /> 
    <asp:RadioButton ID="RadioButton2" runat="server" Text="2" 
     Enabled="false" /> 

    <asp:Button ID="Button1" runat="server" Text="Button" 
     onclick="Button1_Click1" /> 

    <asp:Label ID="Label1" runat="server" Text="" /> 
    </form> 
</body> 
</html> 
在.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication1 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     static readonly string GROUP_NAME = "RadioButtonGroup"; 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      RadioButton1.GroupName = GROUP_NAME; 
      RadioButton2.GroupName = GROUP_NAME; 
      if (IsPostBack) 
      { 
       if (CheckBox1.Checked) 
       { 
        RadioButton1.Enabled = true; 
        RadioButton2.Enabled = true; 
        if (Request.Params[GROUP_NAME] == RadioButton1.ID) 
        { 
         RadioButton1.Checked = true; 
        } 
        else if (Request.Params[GROUP_NAME] == RadioButton2.ID) 
        { 
         RadioButton2.Checked = true; 
        } 
       } 

      } 
     } 

     protected void Button1_Click1(object sender, EventArgs e) 
     { 
      if (Request.Params[GROUP_NAME] == RadioButton1.ID) 
      { 
       Label1.Text = "1 is selected"; 
       if (Request.Params[GROUP_NAME] == RadioButton2.ID) 
       { 
        Label1.Text += "and 2 is selected"; 
       } 
      } 
      if (Request.Params[GROUP_NAME] == RadioButton2.ID) 
      { 
       Label1.Text = "2 is selected"; 
      } 
     } 
    } 
}