我知道下面的代碼並不整齊,但它完成了工作(如果我正確地理解了這個問題)。我在這裏複製/粘貼整個文件內容,讓你更容易玩。只需創建一個名爲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";
}
}
}
}
嗨0xDEAD牛肉,只是爲了確保 - 您使用的ASP.net?還是經典的asp?因爲我不完全確定你的標籤。 –