Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a gridview which consist checkbox in it.
I have written the following code in my code behind in OnRowDataBound event--
C#
CheckBox control2 = e.Row.FindControl("chk830") as CheckBox;
        if (null != control2)
        {
            bool chkd = control2.Checked;
            if (chkd)
            {
                control2.BorderWidth = 2;
                control2.BorderColor = System.Drawing.Color.Red;
            }
        }


the checkbox has OnClick="chkclick(this)" javascript.

C#
<script type="text/javascript">

    var num1 = "";
    function chkclick(obj) {

       
            if (num1 != "") {
if (document.getElementById(num1).checked ||(document.getElementById(num1).checked==false)) {
                    document.getElementById(num1).checked = false;
                    
              
                    if (document.getElementById(num1.substring(0, num1.indexOf("chkV")) + "chk830").style.BorderColor == "")
                        document.getElementById(num1.substring(0, num1.indexOf("chkV")) + "chk830").checked = false;
      }
    }
   }
  </script>

this doesnt work----
C#
if (document.getElementById(num1.substring(0, num1.indexOf("chkV")) + "chk830").style.BorderColor == "")

i also tried--
C#
if (document.getElementById(num1.substring(0,num1.indexOf(quot;chkV")) + "chk830").borderColor == "")


what i want2do is uncheck the checkbox if it does not have border color...how can i achieve this??
:( i want to complete this till tommorrow.. plz suggest me something..
Posted
Updated 20-Dec-12 20:30pm
v2
Comments
VishwaKL 21-Dec-12 2:51am    
how you binding checked condition? if you tell that i can try
Suresh Dasari's 21-Dec-12 5:28am    
can you place your .aspx code,
Do you have 2 checkboxes chkV & chk830.

1 solution

Use cssclass for checkbox borders, border colors etc
& check for the classname in javascript as placed in the below code,
i have tested the below code & its working as per your requirement,
verify and comment.

XML
<head runat="server">
    <title>Untitled Page</title>
    <style>
        .TestClass1
        {
            border: 2px solid red;
            padding-top: 4px;
        }
        .TestClass2
        {
            /*
            border: 2px solid black;
            padding-top: 4px;
            */
        }
    </style>
    <script type="text/javascript">

        function chkclick(obj) {
            var num1 = obj.id;
            if (num1 != "") {
                if (document.getElementById(num1).checked || (document.getElementById(num1).checked == false)) {
                    document.getElementById(num1).checked = false;


                    if (document.getElementById(num1.replace("chkV", "chk830")).parentElement.className == "TestClass1") {
                        document.getElementById(num1.replace("chkV", "chk830")).parentElement.className = "TestClass2";
                        document.getElementById(num1.replace("chkV", "chk830")).checked = false;
                    }
                    else {
                        document.getElementById(num1.replace("chkV", "chk830")).parentElement.className = "TestClass1";
                        document.getElementById(num1.replace("chkV", "chk830")).checked = true;
                    }
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField >
                    <ItemTemplate>
                        <asp:CheckBox ID="chkV" runat="server" onclick="chkclick(this)" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField >
                    <ItemTemplate>
                        <asp:CheckBox ID="chk830" runat="server" onclick="chkclick(this)" Checked="true" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>



Change the OnRowDataBound
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           CheckBox control2 = e.Row.FindControl("chk830") as CheckBox;
           if (null != control2)
           {
               if (control2.Checked)
               {
                   control2.Attributes.Add("class", "TestClass1");
                   //control2.BorderWidth = 2;
                   //control2.BorderColor = System.Drawing.Color.Red;
               }
           }
       }
   }
 
Share this answer
 
Comments
Suresh Dasari's 29-Dec-12 3:35am    
do you got the solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900