动态创建复选框列表并获取所选值 [英] dynamic checkboxlist creation and geting selected value
本文介绍了动态创建复选框列表并获取所选值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码用于创建动态复选框列表:
The below code is used to create a dynamic checkbox list:
CheckBoxList SubCategoryChkboxLst;
protected void CategoryChkboxLst_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < CategoryChkboxLst.Items.Count; i++)
{
if (CategoryChkboxLst.Items[i].Selected == true)
{
objdc.ItemID = Convert.ToInt32(CategoryChkboxLst.Items[i].Value);
DataTable dt = new DataTable();
dt = objbl.GetSubCategories(ref objdc);
SubCategoryChkboxLst = new CheckBoxList();
SubCategoryChkboxLst.DataSource = dt;
SubCategoryChkboxLst.ID = "SubCategoryChkboxLst_" + objdc.ItemID;
SubCategoryChkboxLst.DataTextField = "SUB_ITEM_NAME";
SubCategoryChkboxLst.DataValueField = "SUB_ITEM_ID";
SubCategoryChkboxLst.DataBind();
SubCategoryChkboxLst.RepeatColumns = 5;
SubCategoryChkboxLst.RepeatDirection = RepeatDirection.Horizontal;
//lblYear.ID = "lbl_" + li.Value;
//adding to panel for appearing in specified location
SubCategoryPnl.Controls.Add(SubCategoryChkboxLst);
SubCategoryPnl.Controls.Add(new LiteralControl("<br>"));
}
}
}
我在获取价值和收益方面存在问题在保存按钮中找到控件.找到控件后,我得到null
I have a problem in getting value & finding control in save button.When I find the control, I am getting null
protected void SaveImgbtn_Click(object sender, ImageClickEventArgs e)
{
try
{
if (ProductNameCmbBox.SelectedIndex > 0)
{
//karthik written code
for (int i = 0; i < CategoryChkboxLst.Items.Count; i++)
{
if (CategoryChkboxLst.Items[i].Selected == true)
{
objdc.ItemID = Convert.ToInt32(CategoryChkboxLst.Items[i].Value);
string stId="SubCategoryChkboxLst_"+objdc.ItemID;
CheckBoxList cklt = (CheckBoxList)Page.FindControl(stId);
//here I am getting null ie cklt is null
foreach (ListItem item in cklt.Items)
{
if (item.Selected)
{
objdc.subcategory += item.Value;
//string Activities += item.Value;
}
}
}
}
// code end
objdc.insertedby = Session["UserId"].ToString();
int result = objbl.InsertProductscheme(ref objdc);
if (result == 1)
{
GridBindMethod();
Literal li = new Literal();
li.Text = "<script>alert('Data Inserted Sucessfully');</script>";
Page.Controls.Add(li);
Clearfields();
}
else
{
Literal li = new Literal();
li.Text = "<script>alert('Insertion Failed');</script>";
Page.Controls.Add(li);
}
}
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}
}
推荐答案
您应该阅读此链接
单击
点击
点击
you should read this links
Click
Click
Click
这篇关于动态创建复选框列表并获取所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文