2008-10-07 61 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.