請複製文件後面的aspx和c#代碼。然後你嘗試測試。
------------- ASPX碼------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebTest._Default" %>
<!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">
<title></title>
<style type="text/css" >
.req
{
background-color:Red;
}
</style>
<script language="javascript" type="text/javascript">
var rejArr = new Array(), tmpArr;
function CheckStatus(txtId, chkAppId, chkRejId, type) {
if (type == 'A' && document.getElementById(chkAppId).checked) {
document.getElementById(chkRejId).checked = false;
}
else if (type == 'R' && document.getElementById(chkRejId).checked) {
document.getElementById(chkAppId).checked = false;
document.getElementById(txtId).focus();
}
if (!document.getElementById(chkRejId).checked) {
document.getElementById(chkAppId).checked = true;
type = 'A';
}
ListOfText(txtId, type);
}
function ListOfText(txtId, type) {
var len = rejArr.length;
if (type == 'R')
rejArr[len] = txtId;
else {
var idx = 0;
tmpArr = new Array();
for (var i = 0; i < len; i++) {
document.getElementById(rejArr[i]).className = "";
if (txtId != rejArr[i])
tmpArr[idx++] = rejArr[i];
}
rejArr = tmpArr;
}
}
function CheckReject() {
var txtObj;
if (rejArr.length > 0) {
for (var i = 0; i < rejArr.length; i++) {
txtObj = document.getElementById(rejArr[i]);
if (txtObj != null && txtObj.value == "") {
txtObj.className = "req";
}
}
alert("Please provide reason of reject.");
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_RowDataBound">
<Columns>
<asp:BoundField DataField="NAME" HeaderText="Name" />
<asp:TemplateField HeaderText="Message">
<ItemTemplate>
<asp:TextBox ID="txtMsg" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Message">
<ItemTemplate>
<asp:CheckBox ID="chkApp" runat="server" Text="Approve" Checked="true" />
<asp:CheckBox ID="chkRej" runat="server" Text="Reject" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return CheckReject();" />
</form>
</body>
</html>
------ ------ c#後面的代碼--------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
DataTable dt = new DataTable();
dt.Columns.Add("NAME");
DataRow dr = dt.NewRow();
dr[0] = "James";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Paul";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "John";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Williams";
dt.Rows.Add(dr);
gv.DataSource = dt;
gv.DataBind();
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtCtrl = (TextBox)e.Row.FindControl("txtMsg");
//get approve check box control
CheckBox chkApp = (CheckBox)e.Row.FindControl("chkApp");
//get reject checkbox control
CheckBox chkRej = (CheckBox)e.Row.FindControl("chkRej");
chkApp.Attributes.Add("onclick", "CheckStatus('" + txtCtrl.ClientID + "','" + chkApp.ClientID + "','" + chkRej.ClientID + "','A')");
chkRej.Attributes.Add("onclick", "CheckStatus('" + txtCtrl.ClientID + "','" + chkApp.ClientID + "','" + chkRej.ClientID + "','R')");
}
}
}
}
對於#1,爲什麼你沒有隻使用分組單選按鈕? – rikitikitik 2012-02-01 06:43:51
一旦選擇,我們可以在選項之間切換但不取消選擇它。實際上,我有這個網格來批准或拒絕項目..如果有許多項目我可以批准和拒絕一些,離開休息。我有一個副詞。取消選中這兩個checkoxs,即使在檢查它後,我認爲無法使用單選按鈕。 – user1182062 2012-02-01 07:10:22