简体   繁体   English

将javascript事件添加到输入复选框以在asp.net的代码后执行,

[英]Add javascript event to input checkbox to execute in codebehind asp.net,

I have a html control in aspx page. 我在aspx页面中有一个html控件。

  <input type="checkbox" name="Booking" id="chkBooking" class="css-checkbox" runat="server" /><label for="chkBooking" class="css-label">Booking</label>

I would like to add a javascript event when checkbox is clicked, and that event to be executed in codebehind, like this example here I found on the web: 我想在单击复选框时添加一个javascript事件,该事件将在代码隐藏中执行,就像我在网上找到的以下示例一样:

TextBox1.Attributes.Add("onkeyup", displayControlName + 
    ".innerText=this.value.length;");

How can I call that event also in the code behind? 如何在后面的代码中调用该事件?

Don't really understand this question. 不太了解这个问题。 You can set AutoPostback="true" on an asp.net Checkbox and set its onCheckedChanged event. 您可以在asp.net复选框上设置AutoPostback =“ true”并设置其onCheckedChanged事件。

<asp:CheckBox id="cb" OnCheckedChanged="cb_CheckedChanged" AutoPostBack="true" runat="server">

and, in the code behind 并且,在后面的代码中

protected void cb_CheckedChanged(object sender, EventArgs e)
{
//do whatever you want
}

alertnatively, you can wire up a javascript event to click a button if you want - but why do that when it already has the ability to do a postback? 机敏的是,如果需要,您可以连接一个javascript事件以单击一个按钮-但是当它已经具有回发功能时为什么要这样做呢?

<input type="checkbox" onclick="submitit()">
<asp:Button ID="btn" runat="server" OnClick="btn_Click" Height="0" Width="0" CssClass="hidden" />

<script type="text/javascript">
function submitit()
{
document.getElementById('btn').click();
}

Then, when the checkbox is clicked, the javascript function clicks the button, the page postbacks and btn_Click runs. 然后,当单击复选框时,javascript函数将单击按钮,页面回发并运行btn_Click。

protected void btn_Click(object sender, EventArgs e)
{
//do whatever you want
}

You can add javascript on checked event 您可以在选中的事件上添加JavaScript

if(chkBooking.Checked)
{
    String scriptText = String.Empty;
    scriptText += "function ShowAlert(){";
    scriptText += "  alert( " + 
        " document.forms[0].TextBox1.value";
    scriptText += ")}";

    ClientScript.RegisterClientScriptBlock(this.GetType(), 
       "CounterScript", scriptText, true);
 }

Hope this helps. 希望这可以帮助。

Create an asp:HiddenField (in this case ID is hiddenField) and fetch the checkbox value using the jQuery code below: 创建一个asp:HiddenField(在这种情况下,ID为hiddenField),并使用以下jQuery代码获取复选框值:

It works for me. 这个对我有用。 you can view more from this question [question] Get checkbox value in jQuery 您可以从这个问题中查看更多[问题] 在jQuery中获取复选框值

$('#chkBooking').click(function (e) {
    e.preventDefault();            
        $('#hiddenField').val($('#chkBooking').val());
    $("form").submit();
        });

First of all if you want to call a server side function on Button click then why not going for a server side button control and call your desired function when button's OnClick event fires. 首先,如果您想在Button click上调用服务器端函数,那么为什么不触发服务器端按钮控件,并在Button的OnClick事件触发时调用所需的函数。

YourPage.aspx YourPage.aspx

<asp:Button runat="server" ID="BigButton" OnClick="BigButton_Click" Text="Big Button" />

YourPage.aspx.cs YourPage.aspx.cs

protected void BigButton_Click(object sender, EventArgs e)
    {
        //Call your function from here
    }

Note : You can add css on server side controls using CssClass attributes. 注意: 您可以使用CssClass属性在服务器端控件上添加css。

<asp:Label CssClass="bingo" ID="displayLabel" runat="server" />

Or try this answer : 或尝试以下答案:

How to call a C# function from javascript? 如何从javascript调用C#函数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM