0

我在 asp.net gridview 中有“PlaceHolder”,在那个占位符中我想添加带有多项选择的下拉列表。在代码隐藏中,我添加了一个包含“复选框”的 div,因此当用户单击下拉 div 时,将打开复选框。但在 javascript 中,我无法使用 grid.getElementById 获取 div。

这是使用复选框创建下拉菜单的代码隐藏:

DropDownList ddl = new DropDownList();
        ddl.ID = "ddlChkList";
        ListItem lstItem = new ListItem();
        ddl.Items.Insert(0, lstItem);
        ddl.Width = new Unit(155);
        ddl.Attributes.Add("onmousedown", "showdivonClick()");
        CheckBoxList chkBxLst = new CheckBoxList();
        chkBxLst.ID = "chkLstItem";
        chkBxLst.Attributes.Add("onmouseover", "showdiv()");

        //DataTable dtListItem = ListPrograms();

        //int rowNo = dtListItem.Rows.Count;
        int rowCount = programs.Count;
        string lstValue = string.Empty;
        string lstID = string.Empty;

        int i = 0;

        foreach (AlmsPrograms program in programs)
        {
            i++;
            lstValue = program.PROGRAMNAME;
            lstID = program.PROGRAMID.ToString();
            lstItem = new ListItem("<a href=\"javascript:void(0)\" id=\"alst\" style=\"text-decoration:none;color:Black; \" onclick=\"getSelectedItem(' " + lstValue + "','" + i + "','" + lstID + "','anchor');\">" + lstValue + "</a>", program.PROGRAMID.ToString());
            lstItem.Attributes.Add("onclick", "getSelectedItem('" + lstValue + "','" + i + "','" + lstID + "','listItem');");
            chkBxLst.Items.Add(lstItem);
        }

        System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
        div.ID = "divChkList";
        div.Controls.Add(chkBxLst);
        div.Style.Add("border", "black 1px solid");
        div.Style.Add("width", "250px");
        div.Style.Add("height", "180px");
        div.Style.Add("overflow", "AUTO");
        div.Style.Add("display", "none");
        phDDLCHK.Controls.Add(ddl);
        phDDLCHK.Controls.Add(div);

然后在 aspx 文件中,我尝试使用以下代码获取该 div:

function showdivonClick() {
            var gridViewCtID = '<%=gvProgramSelection.ClientID %>';

            var grid = document.getElementById(gridViewCtID);
            var objDLL = document.getElementById(gridViewCtID + "_divChkList");
            //var objDLL = grid.getElementById('divChkList');
            if (objDLL.style.display == "block")
                objDLL.style.display = "none";
            else
                objDLL.style.display = "block";
        }

但网格还在“divChkList”之前添加了“ctl01”、“ctl02”。如何获得这些动态创建的名称?

谢谢。

4

1 回答 1

0

Set the ClientIDMode to Static which won't muck around with your id (but will still muck around with the name) or another quick and easy way is to just add a class to the div and use that as the selector in jQuery instead.

于 2013-01-22T15:47:28.177 回答