Danieboy
Danieboy

Reputation: 4511

Javascript function not reaching other function?

So the code I have is:

PrintPage.aspx

<asp:Button ID="ViewAuctionsButton" OnClientClick="checkValidated();" Text="Visa" CssClass="" runat="server" />

<script type="text/javascript">
function checkValidated() {
        if (document.getElementById('remember').checked) 
        {
            ViewAuctionsButton_Click;
        } 
        else 
        {
            ViewAuctionsButtonChecked_Click;
        }
    }
</script>

PrintPage.aspx.cs

protected void ViewAuctionsButton_Click(object sender, EventArgs e)
{
     *do stuff*
}

protected void ViewAuctionsButtonChecked_Click(object sender, EventArgs e)
{
     *do stuff*
}

I hope you are seeing what I am trying to do here. "remember" is a checkbox but that's not really important because I know for a fact that when I click the button it will run the if / else code in checkValidated. (I tried it with alert("") boxes.).

Now I have no idea how to make this run beucase it doesn't seem to react to the "ViewAuctionsButtonChecked_Click;".

But if I instead change the code to following:

<asp:Button ID="ViewAuctionsButton" OnClick="ViewAuctionsButtonChecked_Click;" Text="Visa" CssClass="" runat="server" />

Then it will run. But then I am missing the part about the checkbox being checked or not. Any ideas on how I can fix this?

Thanks in advance.

Upvotes: 0

Views: 61

Answers (1)

McGarnagle
McGarnagle

Reputation: 102723

You should make the "remember" checkbox a server-side control, ie:

<asp:CheckBox ID="RememberCheckBox" ... />

That way you can use OnClick="ViewAuctionsButton_Click;", and get the checkbox's value inside that method:

protected void ViewAuctionsButton_Click(object sender, EventArgs e)
{
    if (RememberCheckBox.Checked)
        // do stuff
    else
        // do stuff
}

Upvotes: 2

Related Questions