Friday, September 17, 2010

Select only one check boxes and get that value on server side with the help of javascript [ using master pages ]

Javascript code:

<script language="javascript" type="text/javascript">

     function checkBoxValidate(cb) {
         var childs = document.getElementsByTagName('input');
         for (var i = 0; i < childs.length; i++) {
             if (childs[i].type == 'checkbox') {
                 if (childs[i].id != cb) {
                     childs[i].checked = false;
                 }
             }
         }
     } 
    </script>

---------------------------------------------------------------------------------------------------

Define check boxes in the html side


<input type="checkbox" id="ckbox0" runat="server" checked="true" name="ckbox1" value="0" onclick="javascript:checkBoxValidate(this.id)" />
 All
<input type="checkbox" id="ckbox1" runat="server" name="ckbox2" value="1" onclick="javascript:checkBoxValidate(this.id)" />
 S<input type="checkbox" id="ckbox2" runat="server" name="ckbox3" value="2" onclick="javascript:checkBoxValidate(this.id)" />
 M<input type="checkbox" id="ckbox3" runat="server" name="ckbox4" value="3" onclick="javascript:checkBoxValidate(this.id)" />
 T<input type="checkbox" id="ckbox4" runat="server" name="ckbox5" value="4" onclick="javascript:checkBoxValidate(this.id)" />
 W<input type="checkbox" id="ckbox5" runat="server" name="ckbox6" value="5" onclick="javascript:checkBoxValidate(this.id)" />
 T<input type="checkbox" id="ckbox6" runat="server" name="ckbox7" value="6" onclick="javascript:checkBoxValidate(this.id)" />
 F <input type="checkbox" id="ckbox7" runat="server" name="ckbox8" value="7" onclick="javascript:checkBoxValidate(this.id)" />
 S
---------------------------------------------------------------------------------------------

Find check boxes in the server side in cs page in master pages


        foreach (Control chk in this.Master.FindControl("ContentPlaceHolder1").Controls)
        {
            if (chk.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlInputCheckBox")
            {
                HtmlInputCheckBox Chk1 = (HtmlInputCheckBox)chk;
                if (Chk1.Checked)
                {
                    Chk1.Checked = Chk1.Checked;
                    lbl.Text = Chk1.Value;
                }
            }
        }

 -------------------------its simple page---------------
 foreach (Control chk in this.Page.FindControl("chhhh").Controls) //-- chhh div name
        {
            string a = chk.GetType().ToString();
            if (chk.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlInputCheckBox")
            {
                HtmlInputCheckBox Chk1 = (HtmlInputCheckBox)chk;
                if (Chk1.Checked)
                {
                    Chk1.Checked = Chk1.Checked;
                }
            }
        } 
_____________________________________________________________________

In this way we can find the html side check boxes value in server side using asp.net

No comments:

Post a Comment