Click here to Skip to main content
15,888,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a server form

XML
<asp:checkboxlist id="check1" runat="server">
            <asp:listitem id="option1" runat="server" value="Auto Load" />
    </asp:checkboxlist>
    <input type="button" onclick="test();" runat="server" value="Alterar" />


and then I have this

C#
public string test(){
        string msg = String.Empty;
        if (check1.Items[0].Selected)
        {
            msg = check1.Items[0].Text + "<br />";
        }

        Response.Write(msg);
        return "test" ;
    }



But nothing is executed. What am I doing wrong?
Posted
Updated 26-Dec-10 6:21am
v3
Comments
Manfred Rudolf Bihy 26-Dec-10 12:22pm    
Edit: String.Empty instead of "" for code highlighting neatness.

Use a asp:Button control and handle the click event in the code-behind for the page.
 
Share this answer
 
Hi
Your button "Alterar" is an HTML button. The onclick event (onclick="test();") you have written will try to execute only the javascript function called "test()". It will not fire up the code behind method test(). In order to fire up your code behind you need to use asp.net button control. Some thing like the following

XML
<asp:Button id=Button1 Text="Click Me" onclick="Button1_Click" runat="server" />


and the code behind should be

C#
void Button1_Click(object Source, EventArgs e)
       {
          Response.Write("You clicked the button");
       }
 
Share this answer
 
As your button is HTML button, you need to assign your event to onserverclick as

<input id="Button1" type="button" runat="server" value="Alterar" onserverclick="test"/>


and change your server side code as
C#
protected void test(object sender, EventArgs e)
    {
        string msg = String.Empty;
        if (check1.Items[0].Selected)
        {
            msg = check1.Items[0].Text + "<br />";
        }
        Response.Write(msg);
    }

It'll work.
 
Share this answer
 
Change this:
public string test(){

to

function test(){

What you are doing is calling a client side JS function. You don't need to write public string thing. A normal JS method that can return anything.
 
Share this answer
 
Comments
Maxdd 7 26-Dec-10 20:20pm    
I used with public because I got button is inaccessible due to its protection level.
onclick="teste();"

Spelling mistake - the method name should be test().
 
Share this answer
 
Comments
Maxdd 7 26-Dec-10 12:19pm    
This mistake its only here, its correct in my code, so still dont work.
JF2015 26-Dec-10 13:34pm    
No reason to downvote - have my 5.
Maxdd 7 26-Dec-10 21:00pm    
It wasnt me who downvoted...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900