我想在datatable
CheckListBox
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
我查了this article但对答案不满意
答案 0 :(得分: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
}
这可能适合你
答案 1 :(得分:0)
获得解决方案here
只需创建一个类
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);
}
就是这样,现在我可以获得属性的值