2008-10-07 63 views
5

我有一个网页上这样的CheckBoxList:CheckBoxList的DataValueField值存储在哪里?

<asp:checkboxlist runat="server" id="Locations" datasourceid="LocationsDatasource" 
    datatextfield="CountryName" datavaluefield="CountryCode" /> 

我想通过客户端上的复选框元素循环使用Javascript并抓住每一个选中的复选框的价值,但价值不会出现被可在客户端使用。 HTML输出如下:

<table id="ctl00_Content_Locations" class="SearchFilterCheckboxlist" cellspacing="0" cellpadding="0" border="0" style="width:235px;border-collapse:collapse;"> 
<tr> 
    <td><input id="ctl00_Content_Locations_0" type="checkbox" name="ctl00$Content$Locations$0" /><label for="ctl00_Content_Locations_0">Democratic Republic of the Congo</label></td> 
</tr><tr> 
    <td><input id="ctl00_Content_Locations_1" type="checkbox" name="ctl00$Content$Locations$1" /><label for="ctl00_Content_Locations_1">Central African Republic</label></td> 
</tr><tr> 
    <td><input id="ctl00_Content_Locations_2" type="checkbox" name="ctl00$Content$Locations$2" /><label for="ctl00_Content_Locations_2">Congo</label></td> 
</tr><tr> 
    <td><input id="ctl00_Content_Locations_3" type="checkbox" name="ctl00$Content$Locations$3" /><label for="ctl00_Content_Locations_3">Cameroon</label></td> 
</tr><tr> 
    <td><input id="ctl00_Content_Locations_4" type="checkbox" name="ctl00$Content$Locations$4" /><label for="ctl00_Content_Locations_4">Gabon</label></td> 
</tr><tr> 
    <td><input id="ctl00_Content_Locations_5" type="checkbox" name="ctl00$Content$Locations$5" /><label for="ctl00_Content_Locations_5">Equatorial Guinea</label></td> 
</tr> 

被发现( “CD”, “CG”, “GA”,等等)都远不值。他们在哪?是否有可能在客户端访问它们,还是我需要使用中继器或其他东西自己构建此复选框列表?

回答

2

存储在ViewState中,无法在没有some hacking的客户端上访问它们。

+0

谢谢。我想我只需要用中继器构建复选框列表。 – 2008-10-08 00:35:19

-1

我以前搜索过这个,你可能会接近小运气。我想我记得看到没有javascript的复选框列表项,所以它不理解它。您必须在网页上搜索具有复选框类型的项目,然后测试以查看它属于哪个组(我认为这是属性)。

我搜索我的代码,看看能不能找到我做了什么......


当然,我无法找到我做了什么。

你为什么要在javascript中做客户端?你为什么不做一些AJAX并控制所有服务器端?

+0

供参考:Java和JavaScript是不一样的,它们是完全不同的语言。 – 2008-10-07 20:38:55

+0

这不是一个答案,而是一个意见。应该删除。 – Fandango68 2016-06-07 05:17:43

2

为了避免黑客复选框列表只使用一个中继器这样:

<asp:Repeater ID="rptItems" runat="server" DataSourceID="odsDataSource"> 
<ItemTemplate> 
<input id="iptCheckBox" type="checkbox" runat="server" value='<%# Eval("Key") %>'><%# Eval("Value") %></input> 
</ItemTemplate> 
</asp:Repeater> 
6
<body> 
    <form id="form1" runat="server"> 
      <div> 
       <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="tx" DataValueField="vl"> 
       </asp:CheckBoxList> 
      </div> 
      <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /> 
    </form> 
</body> 
<script type="text/javascript"> 

function Button1_onclick() 
{ 
var itemarr = document.getElementById("CheckBoxList1").getElementsByTagName("span"); 
var itemlen = itemarr.length; 
    for(i = 0; i <itemlen;i++) 
    { 
      alert(itemarr[i].getAttribute("dvalue")); 
    } 
return false; 
} 


</script> 

代码

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     DataTable dt = new DataTable(); 
     dt.Columns.Add("tx"); 
     dt.Columns.Add("vl"); 
     DataRow dr = dt.NewRow(); 
     dr[0] = "asdas"; 
     dr[1] = "1"; 
     dt.Rows.Add(dr); 
     dr = dt.NewRow(); 
     dr[0] = "456456"; 
     dr[1] = "2"; 
     dt.Rows.Add(dr); 
     dr = dt.NewRow(); 
     dr[0] = "yjryut"; 
     dr[1] = "3"; 
     dt.Rows.Add(dr); 
     dr = dt.NewRow(); 
     dr[0] = "yjrfdgdfgyut"; 
     dr[1] = "3"; 
     dt.Rows.Add(dr); 
     dr = dt.NewRow(); 
     dr[0] = "34534"; 
     dr[1] = "3"; 
     dt.Rows.Add(dr); 
     CheckBoxList1.DataSource = dt; 
     CheckBoxList1.DataBind(); 
     foreach (ListItem li in CheckBoxList1.Items) 
     { 
      li.Attributes.Add("dvalue", li.Value); 
     } 
    } 
} 
0

Swapna,你的回答绝对有效。因此,为了检查是否在ASP.Net的CheckBoxList复选框被选中,然后累积列表作为一个逗号分隔的字符串,你可以做

C#代码隐藏

ChkboxList1.DataSource = dsData; 
ChkboxList1.DataTextField = "your-display-column-name"; 
ChkboxList1.DataValueField = "your-identifier-column-name"; 
ChkboxList1.DataBind(); 

foreach (ListItem li in ChkboxList1.Items) 
{ 
    li.Attributes.Add("DataValue", li.Value); 
} 

然后在使用Javascript做

var selValues = ""; 
var ChkboxList1Ctl = document.getElementById("ChkboxList1"); 
var ChkboxList1Arr = null; 
var ChkboxList1Attr= null; 

if (ChkboxList1Ctl != null) 
{ 
    ChkboxList1Arr = ChkboxList1Ctl.getElementsByTagName("INPUT"); 
    ChkboxList1Attr = ChkboxList1Ctl.getElementByTagName("span"); 
} 
if (ChkboxList1Arr != null) 
{ 
    for (var i = 0; i < ChkboxList1Arr.length; i++) 
    { 
     if (ChkboxList1Arr[i].checked) 
      selValues += ChkboxList1Attr[i].getAttribute("DataValue") + ","; 
    } 
    if (selValues.length > 0) 
     selValues = selValues.substr(0, selValues.length - 1); 
} 
2

必须做一些事情真的讨厌为了得到这个工作..

 <asp:Repeater ID="rptItems" runat="server"> 
      <ItemTemplate> 
       <input ID="iptCheckBox" type="checkbox" runat="server" value='<%# Eval("your_data_value") %>' /> 
       <label ID="iptLabel" runat="server"><%# Eval("your_data_field") %></label> 
       <br /> 
      </ItemTemplate> 
     </asp:Repeater> 

代码隐藏:

Private Sub rptItems_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptItems.ItemDataBound 
    Dim checkBox As HtmlInputCheckBox = DirectCast(e.Item.FindControl("iptCheckBox"), HtmlInputCheckBox) 
    Dim label As HtmlControl = DirectCast(e.Item.FindControl("iptLabel"), HtmlControl) 

    label.Attributes.Add("for", checkBox.ClientID) 
End Sub 
15

我终于有我一直在寻找的答案!

asp.net CheckboxList控件确实实际上将值属性呈现为HTML - 它已在制作网站上为我工作了一年多了!(我们总是有EnableViewState关闭所有我们的网站,它仍然工作,没有任何调整或黑客)

然而,突然间,它有一天停止工作,我们的CheckboxList复选框不再呈现他们的价值属性在HTML中!跆拳道你说?我们也是!我们花了一段时间才弄明白,但由于之前一直在工作,我们知道必须有一个原因。原因是我们的web.config发生意外更改!

<pages controlRenderingCompatibilityVersion="3.5"> 

我们从页面配置部分删除了这个属性,并且做了诀窍!

这是为什么?我们反映了CheckBoxList控件的代码,发现这个在RenderItem方法:

if (this.RenderingCompatibility >= VersionUtil.Framework40) 
{ 
    this._controlToRepeat.InputAttributes.Add("value", item.Value); 
} 

亲爱的dev的兄弟姐妹们不要绝望!所有我在Stackexchange上搜索的答案和网络的其他部分给出了错误的信息!由于的.NET 4.0 asp.net渲染的CheckBoxList的复选框的值属性:

<input type="checkbox" value="myvalue1" id="someid" /> 

也许没有实际用途,微软给了你一个“controlRenderingCompatibilityVersion”添加到你的web.config把这种能力通过设置为低于4的版本,这对我们的目的完全没有必要,实际上也是有害的,因为javascript代码依赖于值属性...

我们将chk.val()等于“on”对于我们所有的复选框,这是最初提醒我们这个问题的原因(使用获取复选框值的jquery.val()。结果“on”是被选中的复选框的值。学一些事情每天)。

0

删除<pages controlRenderingCompatibilityVersion="3.5">在我正在处理的一些旧代码中导致了问题。我得到this error。一旦我发现了这个错误,我意识到这样做太冒险了。

为了解决这个问题,我结束了延伸CheckBoxList并重写Render方法添加包含该值到每个列表项的附加属性:

public class CheckBoxListExt : CheckBoxList 
{ 
    protected override void Render(HtmlTextWriter writer) 
    { 
     foreach (ListItem item in this.Items) 
     { 
      item.Attributes.Add("data-value", item.Value); 
     } 

     base.Render(writer); 
    } 
} 

这将使data-value属性在外span标签。

0
Easy Way I Do It. 

//First create query directly From Database that contains hidden field code format (i do it in stored procedure) 

SELECT Id,Name + '<input id="hf_Id" name="hf_Id" value="'+convert(nvarchar(100),Id)+'" type="hidden">' as Name FROM User 

//Then bind checkbox list as normally(i bind it from dataview). 

cbl.DataSource = dv; 
cbl.DataTextField = "Name"; 
cbl.DataValueField = "Id"; 
cbl.DataBind(); 

//Then finally it represent code as follow. 

<table id="cbl_Position" border="0"> 
<tbody> 
<tr> 
<td> 
<input id="cbl_0" name="cbl$0" type="checkbox"> 
<label for="cbl_0"> 
ABC 
<input id="hf_Id" name="hf_Id" value="1" type="hidden"> 
</label> 
</td> 
</tr> 
</table> 

This way you can get DataValueField as inside hiddenfield and also can get it value from client side using javascript.