ASP.NET में checkbox control

asp net checkbox control with code example

CheckBox control एक asp.net web server control है। CheckBox control web forms पर एक वर्ग (square) के रूप में दिखाई देता है | CheckBox control user को वर्ग(square) में check करने और uncheck करने की अनुमति देता है। यदि CheckBox control में वर्ग(square) को tick किया जाता है तो checked = true और unchecked है तो checked = false होता है |
हम toolbox से CheckBox control को drag कर सकते हैं और इसे नीचे दिखाए गए अनुसार web form पर दिखा सकते हैं। CheckBox control उपयोगकर्ता को asp.net में Checkbox control को या तो check (tick) या uncheck (untick) करने की अनुमति देता है।

ASP.Net C# में CheckBox Control का उदाहरण

Checkbox को check किया गया है या uncheck किया गया है यह समझने के लिए एक साधारण asp.net में checkbox से समन्धित उदाहरण लेते है|

aspnet check box control
यहां asp.net में web page का html design source code है :-

<body>
<form id="form1" runat="server">
<div>
<table align="center" class="style1" style="border: thin solid #008080">
<tr>
<td class="style2" style="text-align: center; border-bottom-style: solid; border-bottom-width: thin; border-bottom-color: #008080;">
CheckBox Control in ASP.Net</td>
</tr>
<tr>
<td style="text-align: center">
</td>
</tr>
<tr>
<td style="text-align: center">
<asp:CheckBox ID="CheckBox1" runat="server" Text="RED" />
</td>
</tr>
<tr>
<td style="text-align: center">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Select" />
</td>
</tr>
<tr>
<td style="text-align: center">
<asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>

एक checkbox के साथ asp.net web page को डिजाइन करने के बाद, button और label control नीचे दी हुई screen की तरह दिखाई देता है। नीचे दिए गए Asp.net figure में हमने checkbox को check नहीं किया | जब select button पर click किया जाता है तो हमें label control में जवाब मिलता है कि checkbox unchecked है।

aspnet checkbox control properties

अब, checkbox control को check करें और फिर label में सन्देश (message) को प्रदर्शित करने के लिए button पर click आप label पर कोई भी सन्देश (message) प्रदर्शित करा सकते है जैसे – “CheckBox is Checked”

यहाँ checkbox को check करने के लिए button click event पर c# कोड दिया गया है जिसमे यह बताया गया है कि checkbox को check किया गया है या uncheck |


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default9 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (CheckBox1.Checked == true)
        {
            Label1.Text = "CheckBox is Checked";
        }
        else
        {
            Label1.Text = "CheckBox is unchecked";
        }
    }
}

Checkbox Property Autopostback

ऑटोपोस्टबैक का मतलब है जब हम उपयोगकर्ता से अनुरोध प्राप्त करते हैं और अनुरोध के साथ सर्वर पर जाते हैं और सर्वर पर अनुरोध को आगे बढ़ाते हैं और परिणामस्वरूप प्रतिक्रिया के साथ उपयोगकर्ता को वापस मिलता है, सभी राउंड ट्रिप जिसे पोस्टबैक के रूप में जाना जाता है।

अगर हम चेक बॉक्स पर Autopostback = true सेट करते हैं तो चेकबॉक्स नियंत्रण CheckedChanged ईवेंट उपयोगकर्ता के लिए सक्षम करता है, अर्थात हम चेकबॉक्स नियंत्रण को चेक या अनचेक करते समय पीछे कोड में जाते हैं।

नीचे दिए गए उदाहरण से पता चलता है कि चेकबॉक्स नियंत्रण में ऑटोपोस्टबैक कैसे काम करेगा। एक लेबल नियंत्रण के साथ दो चेकबॉक्स नियंत्रण के साथ asp.net वेब पेज डिज़ाइन करें।

दोनों checkbox control AutoPostBack = true पर set करें।

ASP.Net में CheckBox Control AutoPostBack उदाहरण

<body>
    <form id="form1" runat="server">
    <div>
    
        <table align="center" class="style1" style="border: thin solid #008080">
            <tr>
                <td class="style2" 
                    style="text-align: center; border-bottom-style: solid; border-bottom-width: thin; border-bottom-color: #008080;">
                    CheckBox Control in ASP.Net</td>
            </tr>
            <tr>
                <td style="text-align: center">
 
                     
                    </td>
            </tr>
            <tr>
                <td style="text-align: center">
                    <asp:CheckBox ID="chkred" runat="server" AutoPostBack="True" 
                        oncheckedchanged="chkred_CheckedChanged" style="font-weight: 700" Text="RED" />
 
                     
                    <asp:CheckBox ID="chkgreen" runat="server" AutoPostBack="True" 
                        oncheckedchanged="chkgreen_CheckedChanged" style="font-weight: 700" 
                        Text="GREEN" />
                </td>
            </tr>
            <tr>
                <td style="text-align: center">
                     </td>
            </tr>
            <tr>
                <td style="text-align: center">
                    <asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label>
                </td>
            </tr>
            </table>
    
    </div>
    </form>
</body>
</html>

यहां, हम एक बार में केवल एक checkbox की जांच कर सकते हैं, अगर हम एक checkbox को check करते हैं और अन्य checkbox की जांच करने का प्रयास करते हैं तो checkbox_CheckedChanged events का उपयोग करके पहले checkbox control स्वचालित रूप से unchecked हो जाते है।


नीचे screen में asp.net checkbox autopostback उदाहरण का design output दिखाता है।

asp net checkbox autopostback

दोनों checkbox CheckedChanged event के लिए नीचे दिए गए कोड को देखे |

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default9 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void chkred_CheckedChanged(object sender, EventArgs e)
    {
        Label1.Text = "RED CheckBox is checked";
        chkgreen.Checked = false;
    }
    protected void chkgreen_CheckedChanged(object sender, EventArgs e)
    {
        Label1.Text = "GREEN CheckBox is checked";
        chkred.Checked = false;
    }
}

नीचे दिए गए उदाहरण में जब हम Red checkbox control को check करते हैं तो label control पर “RED CheckBox is checked” संदेश प्रदर्शित होता है। और यदि हम green checkbox control को check करते हैं तो Red checkbox control स्वचालित रूप से unchecked हो जाता है और label control green checkbox के साथ checked सन्देश प्रदर्शित करता है|


error: Content is protected !!