使用复选框 [英] Using checkbox

查看:85
本文介绍了使用复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器表格

I have a server form

<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

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?

推荐答案

使用asp:Button控件并处理页面代码背后的click事件.
Use a asp:Button control and handle the click event in the code-behind for the page.



您的"Alterar"按钮是一个HTML按钮.您编写的onclick事件(onclick ="test();")将尝试仅执行名为"test()"的javascript函数.它不会触发方法test()后面的代码.为了在后面触发代码,您需要使用asp.net按钮控件.类似以下内容

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

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



后面的代码应该是



and the code behind should be

void Button1_Click(object Source, EventArgs e)
       {
          Response.Write("You clicked the button");
       }


由于您的按钮是HTML按钮,因此您需要将事件分配给onserverclick作为

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

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.


这篇关于使用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆