access(FindControl)动态创建复选框 [英] access(FindControl) dynamically created checkboxs

查看:117
本文介绍了access(FindControl)动态创建复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个列表视图。我在< itemtemplate>中添加了占位符列表视图。我动态创建了占位符内的复选框。



有一个按钮,点击其中我想显示所有选中复选框的值。怎么做?



Hi,

I have a listview. I have added placeholder inside <itemtemplate> of listview. I am dynamically creating checkboxed inside placeholder.

there is a button, on click of which i want to display values of all checked checkboxes. How to do this?

protected void lvwFields_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            PlaceHolder plhldFields = (PlaceHolder)e.Item.FindControl("plhldFields");

            ListViewDataItem item = (ListViewDataItem)e.Item;
            DataRowView dvw = (DataRowView)item.DataItem;

            CheckBox cb = new CheckBox();

            cb.ID = dvw["Val"].ToString();
            cb.Text = dvw["Txt"].ToString();
            cb.ToolTip = dvw[ViewState["FieldName"].ToString()].ToString();

            plhldFields.Controls.Add(cb);

           showSubFields(plhldFields, dvw["fieldname"].ToString(), dvw["t"].ToString());
         }

public void showSubFields(PlaceHolder ph, string strSubFldName, string strTableName)
        {
//after executing dataset
HtmlTableRow tr = new HtmlTableRow();
                            HtmlTableCell tc = new HtmlTableCell();
                            CheckBox chk = new CheckBox();
                            HtmlGenericControl hgen = new HtmlGenericControl();
                            hgen.InnerHtml = "    ";
                            chk.ID = dsSub.Tables[0].Rows[i]["SubVal"].ToString().Trim();
                            chk.Text = dsSub.Tables[0].Rows[i]["SubTxt"].ToString().Trim();
                            chk.ToolTip = dsSub.Tables[0].Rows[i]["disp"].ToString().Trim();
                            tc.Controls.Add(hgen);
                            tc.Controls.Add(chk);
                            tr.Controls.Add(tc);
                            tbl.Controls.Add(tr);
                            ph.Controls.Add(tbl);
}

there is a button on click of that i want to display checked values

protected void btnAdd_Click(object sender, EventArgs e)
{
foreach (ListViewDataItem item in lvwFields.Items)
            {
                PlaceHolder ph = (PlaceHolder)item.FindControl("plhldFields");
                foreach (Control c in ph.Controls) //here i am getting countrols count 0
                {
                    if (c is CheckBox)
                    {

                    }
                }
            }
}





谢谢



Thanks

推荐答案

所有动态控件都会在每个帖子上丢失。所以你需要通过重新绑定listview来重新生成它们(最好是在页面加载中)。之后,你可以从Request对象中获取动态控制值,如下所示



All dynamic controls get lost on every post back. So you need to regenerate them on every postback by rebinding your listview (best is in pageload). After that you can fetch dynamic control value from Request object like below

if (c is CheckBox)
{
    var chkval = "";
    if (Request[c.UniqueID] != null)
    {
      chkval = Request[c.UniqueID];
    }
}


这篇关于access(FindControl)动态创建复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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