最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - My checkbox in asp.net doesn't work with the codebehind file - Stack Overflow

programmeradmin1浏览0评论

I've got a checkbox, which should go to a method in the codebehind file. This is my aspx code:

<input name="fullscreen_chbx" type="checkbox" id="fullscreen_chbx"  onclick="fullscreen_chbx_Click" runat="server"/>

And this is my codebehind code. The code should work but it doesn't even execute the method.

protected void fullscreen_chbx_Click(object sender, EventArgs e)
{
    bool fullscreen = false;


    if (fullscreen == false)
    {
        InputSimulator.SimulateKeyPress(VirtualKeyCode.F11);
    }
    else
    {
        InputSimulator.SimulateKeyPress(VirtualKeyCode.F11);


    } 

There's always this error in the aspx file: runtime error in JavaScript: "fullscreen_chbx_Click" is undefined. Why is there a error with javascript?

I've got a checkbox, which should go to a method in the codebehind file. This is my aspx code:

<input name="fullscreen_chbx" type="checkbox" id="fullscreen_chbx"  onclick="fullscreen_chbx_Click" runat="server"/>

And this is my codebehind code. The code should work but it doesn't even execute the method.

protected void fullscreen_chbx_Click(object sender, EventArgs e)
{
    bool fullscreen = false;


    if (fullscreen == false)
    {
        InputSimulator.SimulateKeyPress(VirtualKeyCode.F11);
    }
    else
    {
        InputSimulator.SimulateKeyPress(VirtualKeyCode.F11);


    } 

There's always this error in the aspx file: runtime error in JavaScript: "fullscreen_chbx_Click" is undefined. Why is there a error with javascript?

Share Improve this question asked Jul 2, 2015 at 11:36 aha364636aha364636 3637 silver badges23 bronze badges 4
  • Your fullscreen_chbx_Click is being looked at as a JS function from the HTML. This is because you are not using a server side control. – Matt Rowland Commented Jul 2, 2015 at 11:39
  • That's not how it works. You cannot directly call back-end methods from JavaScript. – Yeldar Kurmangaliyev Commented Jul 2, 2015 at 11:40
  • @MattRowland But I used runat="server". Doesn't it work? – aha364636 Commented Jul 2, 2015 at 11:42
  • The server doesn't understand what a HTML <input> control is. Check out @TimBJames answer. – Matt Rowland Commented Jul 2, 2015 at 11:44
Add a ment  | 

4 Answers 4

Reset to default 3

You are not using a ServerSide control, so the onclick event is attempting to fire off a JavaScript event.

You can change your code to use a ServerSide control, e.g.

<asp:CheckBox id="fullscreen_chbx" OnCheckedChanged="fullscreen_chbx_Click" runat="server"/>

Notice that there is no onclick event handler for the ServerSide control, so you will need to use OnCheckedChanged. You will also probably want to add AutoPostBack="True" to your control so that the event is triggered straight away.

<asp:CheckBox id="fullscreen_chbx" OnCheckedChanged="fullscreen_chbx_Click" runat="server" AutoPostBack="True" />

Then this will trigger your server event. If you want to add a clientside event to this, then you can always add OnClientClick="somefunction();"

Change your code from to this

<input name="fullscreen_chbx" type="checkbox" id="fullscreen_chbx"  onclick="fullscreen_chbx_Click" runat="server"/>

to this

<asp:CheckBox id="fullscreen_chbx" OnClick="fullscreen_chbx_Click" runat="server"/>

This is because your are using HTML control ans it is not server side control.

To use check box in server side you need to add below code.

 <asp:CheckBox ID="fullscreen_chbx" runat="server" AutoPostBack="True"
oncheckedchanged="fullscreen_chbx_CheckedChanged" />

protected void fullscreen_chbx_CheckedChanged(object sender, EventArgs e)
{
        if (fullscreen_chbx.Checked)
        {

        }
        else
        {

        }

}

AutoPostBack="True"

event of Checkbox control raises only if the AutoPostBack property of checkbox is specified with value "true".

发布评论

评论列表(0)

  1. 暂无评论