1

我也想在不再次调用绑定方法的情况下保留datatableinCheckListBox attributes方法的值postback

所以我有一个asp:CheckBoxList并且我正在绑定它

    if (!IsPostBack)
            {
// code for binding 
    }

ASP.NET

<asp:CheckBoxList ID="chkboxCandidateList" runat="server">
</asp:CheckBoxList>

这是我的绑定方法C#

DataTable dtCandidateName = // datatable having all specified column 
            if (dtCandidateName != null && dtCandidateName.Rows.Count > 0)
            {
                chkLstBxCandidateName.Items.Clear();
                ListItem lstItem = null;
                for (int i = 0; dtCandidateName.Rows.Count > i; i++)
                {
                    lstItem = new ListItem(Convert.ToString(dtCandidateName.Rows[i]["Candidate Name"]), Convert.ToString(dtCandidateName.Rows[i]["Candidate Id"]));
                    lstItem.Attributes.Add("Email", Convert.ToString(dtCandidateName.Rows[i]["Email"]));
                    lstItem.Attributes.Add("Mobile", Convert.ToString(dtCandidateName.Rows[i]["Mobile"]));
                    chkLstBxCandidateName.Items.Add(lstItem);
                }
            }

甚至我在第一次调用页面加载时也得到了值

HTML

<span email="test@kartika.com" mobile="01111111111"><input id="ContentPlaceHolder1_chkboxCandidateList_0" type="checkbox" name="ctl00$ContentPlaceHolder1$chkboxCandidateList$0" checked="checked" value="486"><label for="ContentPlaceHolder1_chkboxCandidateList_0">Kratika Shukla</label></span>

因此,当我单击提交按钮时,我没有获得电子邮件和移动设备的价值

chkboxCandidateList.Items[i].Attributes["Email"] -- getting null

我检查了这篇文章,但对答案不满意

4

2 回答 2

0

试试这个按钮点击

protected void btnSubmit_Click(object sender, EventArgs e)
{
 List<string> values=new List<string>();
foreach (ListItem item in chkboxCandidateList.Items)
if (item.Selected)
  values.Add(item.Text); // retrieve values here 

}

这可能对你有用

于 2013-03-30T06:54:23.640 回答
0

在这里得到了解决方案

只需创建一个类

namespace customControl
{
    public class ClsCheckBoxList : CheckBoxList
    {
        protected override object SaveViewState()
        {
            // create object array for Item count + 1
            object[] allStates = new object[this.Items.Count + 1];

            // the +1 is to hold the base info
            object baseState = base.SaveViewState();
            allStates[0] = baseState;

            Int32 i = 1;
            // now loop through and save each Style attribute for the List
            foreach (ListItem li in this.Items)
            {
                Int32 j = 0;
                string[][] attributes = new string[li.Attributes.Count][];
                foreach (string attribute in li.Attributes.Keys)
                {
                    attributes[j++] = new string[] { attribute, li.Attributes[attribute] };
                }
                allStates[i++] = attributes;
            }
            return allStates;
        }

        protected override void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                object[] myState = (object[])savedState;

                // restore base first
                if (myState[0] != null)
                    base.LoadViewState(myState[0]);

                Int32 i = 1;
                foreach (ListItem li in this.Items)
                {
                    // loop through and restore each style attribute
                    foreach (string[] attribute in (string[][])myState[i++])
                    {
                        li.Attributes[attribute[0]] = attribute[1];
                    }
                }
            }
        }
    }
}

并在 ASP.NET 中为此添加参考

<%@ Register TagPrefix="TRControls" Namespace="customControl" %>

 <TRControls:ClsCheckBoxList ID="chkBox" runat="server">
    </TRControls:ClsCheckBoxList>

用于绑定后面的代码

if (!IsPostBack)
        {
            ListItem lstItem = new ListItem("vikas", "0", true);
            lstItem.Attributes.Add("love", "sure");
            chklstbox.Items.Add(lstItem);
            chkBox.Items.Add(lstItem);
            lstItem = new ListItem("kratika", "1", true);
            lstItem.Attributes.Add("love", "not sure");
            chklstbox.Items.Add(lstItem);
            chkBox.Items.Add(lstItem);
        }

就是这样,现在我可以获取属性的值

于 2013-03-30T07:39:02.817 回答