在SelectedIndexChangeProperty()的CheckBoxList中获取当前Checkbox的状态(已检查/未检查) [英] Get Status(Checked /UnChecked ) of the current Checkbox In CheckBoxList on the SelectedIndexChangeProperty()

查看:86
本文介绍了在SelectedIndexChangeProperty()的CheckBoxList中获取当前Checkbox的状态(已检查/未检查)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于CheckBoxList,我在处理SelectedIndexChanged 事件时遇到问题.当我处理SelectedIndexChanged 事件时,我无法引用被单击的复选框(选中状态"或未选中状态"),因为"SelectedIndex"仅引用第一个选中的复选框.我想要这个,因为我必须做类似的事情:

Hi,

For CheckBoxList, i am having a problem handling SelectedIndexChanged event. When i handle SelectedIndexChanged event, i can''t reference the checkbox that was clicked ( checked or Unchecked Status ) , as the "SelectedIndex" only references the first checked checkbox. I want this, since I have to do something like :

 If ( Current checkbox Status = Checked )
{
    do something
}
elseif(Current checkbox Status = UnChecked )
{
   do something
} 



有没有一种方法可以找到,(1)CheckBoxList中当前CheckBox的SelectedValue/SelectedIndex(打勾/未打勾). (2)要获取SelectedIndexChangeProperty()上当前复选框(已打勾/未打勾)的状态(已选中/未选中)?

在此先感谢,



Is there a way to find, (1) SelectedValue / SelectedIndex of Current CheckBox(ticked/unticked) in CheckBoxList. (2) To get the Status(Checked /UnChecked ) of the current Checkbox(ticked/unticked) on the SelectedIndexChangeProperty()?

Thanks In Advance,

推荐答案

我使用以下示例代码尝试将其完成:

标记:

I used the following sample code to try to get it done:

Markup :

<asp:CheckBoxList ID="CheckBoxList1" AutoPostBack="true" runat="server"

       onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
   </asp:CheckBoxList>



CodeBehind:



CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            ListItem item1 = new ListItem("1", "1");
            ListItem item2 = new ListItem("2", "2");
            ListItem item3 = new ListItem("3", "3");

            CheckBoxList1.Items.Add(item1);
            CheckBoxList1.Items.Add(item2);
            CheckBoxList1.Items.Add(item3);
        }
    }
 
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (CheckBoxList1.SelectedItem != null)
        {
            if (CheckBoxList1.SelectedItem.Selected)
                Response.Write("You checked " + CheckBoxList1.SelectedItem.Text);
            else
                Response.Write("You unchecked " + CheckBoxList1.SelectedItem.Text);
        }
}



在两种情况下都会触发SelectedIndexChanged 事件(复选框check uncheck),并且当您"check"一项(我发现一项)时,Asp.net可以在CheckBoxList 中为您提供正确的选定项.但无法显示正确的复选框).但是,不幸的是,当您"uncheck"一个复选框时,它无法为您提供信息,因为"CheckBoxList1.SelectedItem"为空,并且没有其他合适的属性来获取此信息.

因此,您必须进行一些客户端脚本编写(使用JQuery JavaScripts),以获取已选中或未选中的复选框引用,将该信息存储或发送至代码隐藏,然后使用该信息执行服务器端代码.



The SelectedIndexChanged event is fired in both cases (Check box check and uncheck) and, Asp.net can give you the correct selected item in the CheckBoxList when you "check" one (I found one case however it was failing to show the correct check box). But, unfortunately, when you "uncheck" a check box, it can''t give you the information because the "CheckBoxList1.SelectedItem" is null and there is no other suitable property to get this information.

So, you have to do some client side scripting (Using JQuery or JavaScripts) to get the checked or unchecked check box reference, store or send this information to codebehind and then execute the server side code with that information.


使用

use

if (CheckBoxList1.Items[i].Selected == true)
    {
    }



代替if(CheckBoxList1.SelectedItem!= null)

这将在for循环中

喜欢

for(int i = 0; i< CheckBoxList1.Items.Count; i ++)
{}


希望对您有帮助.



in stead of if (CheckBoxList1.SelectedItem != null)

this will be in a for loop

like

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{}


Hope this will be helpful for you.


这篇关于在SelectedIndexChangeProperty()的CheckBoxList中获取当前Checkbox的状态(已检查/未检查)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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