标签:style blog class code java ext
由于在客户端用js是无法直接获取到ASP.NET的控件CheckboxList的值的,所以采用以下解解方案:
服务器端代码:
public void LoadAllTags() { var tagList = tagBO.GetAllTags(); cbTagList.DataSource = tagList; cbTagList.DataTextField = "TagName"; cbTagList.DataValueField = "Id"; cbTagList.DataBind(); for (int i = 0; i < cbTagList.Items.Count; i++) { cbTagList.Items[i].Attributes.Add("TagId", cbTagList.Items[i].Value); cbTagList.Items[i].Attributes.Add("TagName", cbTagList.Items[i].Text); } }
前台JS代码:
function SetTags() { var ckelems = document.getElementById("cbTagList").getElementsByTagName("input"); var spelems = document.getElementById("cbTagList").getElementsByTagName("span"); var tagIdsStr=""; var tagNamesStr=""; for (var i = 0; i < ckelems.length; i++) { if (ckelems[i].type == "checkbox" && ckelems[i].checked==true) { var tagName = spelems[i].attributes["tagname"].nodeValue; var tagId = spelems[i].attributes["tagid"].nodeValue; tagIdsStr += tagId+","; tagNamesStr += tagName + ","; } } $("#<%=hfTagIds.ClientID %>").val(tagIdsStr.substring(0, tagIdsStr.length-1)); $("#<%=txtTags.ClientID %>").val(tagNamesStr.substring(0, tagNamesStr.length - 1)); }
【Javascript】JS获取ASP.NET CheckBoxList控件的Text和Value,布布扣,bubuko.com
【Javascript】JS获取ASP.NET CheckBoxList控件的Text和Value
标签:style blog class code java ext
原文地址:http://www.cnblogs.com/BruceGoGo/p/3721225.html