我怎样才能获得的CheckBoxList选择的价值观,我有什么似乎并没有工作,C#.NET / VisualWebPart [英] How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart

查看:141
本文介绍了我怎样才能获得的CheckBoxList选择的价值观,我有什么似乎并没有工作,C#.NET / VisualWebPart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个类文件的CheckBoxList和我使用的HtmlTextWriter呈现控件。

我用下面的code到所选择的值存储在一个字符串:

 字符串YrStr =;
的for(int i = 0; I< YrChkBox.Items.Count;我++)
{
    如果(YrChkBox.Items [I] .Selected)
    {
        YrStr + = YrChkBox.Items [I] .value的+;
    }
}

我在code踩,它似乎并没有打的内部if语句和放大器;选择value属性是每一次错误的......任何人都有的我怎么能解决这个问题的想法?

我用下面的它填充:

  YrChkBox.Items.Add(新的ListItem(项目1,项目1));


解决方案

在您的ASPX页面你有名单如下:

 < ASP:的CheckBoxList ID =YrChkBox=服务器
        onselectedindexchanged =YrChkBox_SelectedIndexChanged>< / ASP:&的CheckBoxList GT;
    < ASP:按钮的ID =按钮=服务器文本=提交/>

在aspx.cs页面背后的code,你有这样的:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!的IsPostBack)
        {
            //填充的CheckBoxList的项目,只有当它不是一个回发。
            YrChkBox.Items.Add(新的ListItem(项目1,项目1));
            YrChkBox.Items.Add(新的ListItem(项目2,项目2));
        }
    }    保护无效YrChkBox_SelectedIndexChanged(对象发件人,EventArgs的发送)
    {
        //创建列表存储。
        清单<串GT; YrStrList =新的List<串GT;();
        //循环每一个项目。
        的foreach(在YrChkBox.Items列表项的项目)
        {
            如果(item.Selected)
            {
                //如果选择这个项目时,值添加到列表中。
                YrStrList.Add(item.Value);
            }
            其他
            {
                //项目未被选中,做别的事情。
            }
        }
        //加入串起来使用;分隔符。
        串YrStr =的string.join(;,YrStrList.ToArray());        //写入到页面的价值。
        的Response.Write(String.Concat(选定项目,YrStr));
    }

确保您使用如果(!的IsPostBack){} 的条件,因为如果你加载它的每一个页面刷新,它实际上破坏数据。

I am creating a CheckBoxList in a class file and am using an HTMLTextWriter to render the control.

I'm using the following code to store the selected values in a string:

string YrStr = "";
for (int i = 0; i < YrChkBox.Items.Count; i++)
{
    if (YrChkBox.Items[i].Selected)
    {
        YrStr += YrChkBox.Items[i].Value + ";"; 
    }
}

I stepped through the code and it doesn't seem to hit the inside of the if statement & the selected value attribute is false every time ... Anyone have an idea of how I can address this?

I populate it using the following:

 YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));

解决方案

In your ASPX page you've got the list like this:

    <asp:CheckBoxList ID="YrChkBox" runat="server" 
        onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList>
    <asp:Button ID="button" runat="server" Text="Submit" />

In your code behind aspx.cs page, you have this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Populate the CheckBoxList items only when it's not a postback.
            YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));
            YrChkBox.Items.Add(new ListItem("Item 2", "Item2"));
        }
    }

    protected void YrChkBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Create the list to store.
        List<String> YrStrList = new List<string>();
        // Loop through each item.
        foreach (ListItem item in YrChkBox.Items)
        {
            if (item.Selected)
            {
                // If the item is selected, add the value to the list.
                YrStrList.Add(item.Value);
            }
            else
            {
                // Item is not selected, do something else.
            }
        }
        // Join the string together using the ; delimiter.
        String YrStr = String.Join(";", YrStrList.ToArray());

        // Write to the page the value.
        Response.Write(String.Concat("Selected Items: ", YrStr));
    }

Ensure you use the if (!IsPostBack) { } condition because if you load it every page refresh, it's actually destroying the data.

这篇关于我怎样才能获得的CheckBoxList选择的价值观,我有什么似乎并没有工作,C#.NET / VisualWebPart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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