You must Sign In to post a response.
  • Category: .NET

    Checkbox check status and calling different methods till this status

    i should check status a checkbox, if it is checed method1 should call and run (from codebehind..serverside) if checkbox unchecked method2 should call and run from serverside, i want to use jquery or javascript ( i am usinf .net (c#.net)
  • #769345
    Hi

    You can achieve that using jquery. Have a look on below code


    <input id="chkjobtypes" name="chkjobtypes" type="checkbox" value="status " />

    $('#chkboxid').change(function ()
    {
    if ($("#chkboxid").is(':checked'))
    {
    alert("check box checked");
    var ActionUri = "@Url.Action("method1", "controllername")";
    $.post(ActionUri, null, function (data) {
    });
    }
    else {
    alert("check box unchecked");
    var ActionUri = "@Url.Action("method2", "controllername")";
    $.post(ActionUri, null, function (data) {
    });
    }
    });

    Sridhar Thota.
    Editor: DNS Forum.

  • #769346
    is it calls and runs to serverside methods? (example you have a sridharthota.aspx and if checbox checked itll run sridhar() method if not checed it ll runt thota() method)

  • #769347
    Yes it will work replace in your with your controller name and action method names in the example I have provided.
    It doesn't mean to paste same code in your application, make necessary changes and run your application.

    Sridhar Thota.
    Editor: DNS Forum.

  • #769348
    Have you any working sample about it.. and thank you very much for kindly help

  • #769350
    Hi
    It is for razor engine. If you are using aspx engine in MVC use as

    var ActionUri = "<%:Url.Action("method1", "controllername")%>";
    $.post(ActionUri, null, function (data) {
    });

    Here we will provide you the idea, You should not expect total code which will work for you.
    You should try and modify as necessary from your side by taking the idea.

    Sridhar Thota.
    Editor: DNS Forum.

  • #769353
    No, i am using c# .net no MVC. but thanks for your kindly help.

  • #769354
    Hi
    I understood your requirement.
    1.Add sample.aspx webform to your project , add one checkbox make checkbox autopostback =true and one label make label visible =false , the sample.aspx should look like below


    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sample.aspx.cs" Inherits="AspNet_WebApplication.Sample" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" Text="Status" />
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
    </form>
    </body>
    </html>



    Your sample.aspx.cs should look like below.

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

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

    }
    public void Method1()
    {
    //Your logic in method1
    Label1.Visible = true;
    Label1.Text = "Am from Method1";
    }
    public void Method2()
    {
    //Your logic in method2
    Label1.Visible = true;
    Label1.Text = "Am from Method2";
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
    if (CheckBox1.Checked == true)
    {
    Method1();
    }
    else
    {
    Method2();
    }
    }
    }
    }


    Find the below attachment of out put, when checked it will call method1 if unchecked it will call method2

    Sridhar Thota.
    Editor: DNS Forum.

    Delete Attachment


  • Sign In to post your comments