在gridview中的复选框和按钮单击上的验证之间切换

时间:2012-02-01 06:38:13

标签: javascript asp.net gridview

  1. 我的网格视图包含带有2个复选框列和文本框列的模板字段。 我需要javascript来切换复选框中的检查状态(即我有2个复选框,名称为chkapprove和chkreject,如果我检查“chkapprove”,那么“chkreject”应取消选中是否已经检查过,反之亦然)。

  2. 我需要一个按钮点击功能,当选中chkreject且在文本框中没有输入任何内容时,必须用msg“输入原因”提醒。

  3. function reason() 
    {
        MyGridView = document.getElementById('<%= grdaccess.ClientID %>');
    
        var Inputs = MyGridView.getElementsByTagName("input");
        var chkBox = "Chkreject";
        var textbox = "txtreason";
    
        for (var n = 0; n < Inputs.length; ++n) 
            if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(chkBox, 0) >= 0 && Inputs[n].checked)
                if (Inputs[n].type == 'textbox' && Inputs[n].id.indexOf(textbox, 0) >= 0 && Inputs[n].length <= 0)    
                {
                    alert("enter the reason");
                    return false;
                }
        return true;
    }
    
    <asp:Button ID="btnSubmit" runat="server" CssClass="showall" Text="Submit" OnClientClick="javascript:return reason();" onclick="btnsubmit_Click" />
    

    这是我为条件2编写的代码,但它不起作用。

    任何人都可以帮我吗?

    提前致谢,

    gowrishankar。

1 个答案:

答案 0 :(得分:0)

请复制文件背后的aspx和c#代码。然后你尝试测试。

------------- aspx code ------------------

<%@ 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')");

            }
        }
    }
}