Where are the DataValueField values for a CheckBoxList stored?

前端 未结 10 2069
旧时难觅i
旧时难觅i 2020-12-16 06:42

I have this CheckBoxList on a page:



        
相关标签:
10条回答
  • 2020-12-16 07:14

    To avoid hacking the checkbox list just use a repeater as such:

    <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>
    
    0 讨论(0)
  • 2020-12-16 07:17
    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.
    
    0 讨论(0)
  • 2020-12-16 07:23

    Had to do something really nasty in order to get this to work..

            <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>
    

    Codebehind:

    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
    
    0 讨论(0)
  • 2020-12-16 07:23

    I tried the following and its working for me:

    <asp:CheckBoxList ID="chkAttachments" runat="server"></asp:CheckBoxList>
    

    Server side binding:

    private void LoadCheckBoxData()
            {
    
                var docList = new List<Documents>(); // need to get the list data from database
                        chkAttachments.Items.Clear();
    
                        ListItem item = new ListItem();
                      
                        foreach (var doc in docList )
                        {
                            item = new ListItem();
                            item.Value = doc.Id.ToString();
                            item.Text = doc.doc_name;
                            chkAttachments.Items.Add(item);
                        }
                    }
        
    
    0 讨论(0)
提交回复
热议问题