Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ASP CheckBoxList labels stay on same line as checkbox

This may be a common problem but I'm struggling to find a solution that will fix it

I have a modal popup I am displaying with jQuery, this popup contains a list of Checkboxes and a Button, the code looks like:

<div id="dialog" title="Notify Users" >
    <div style="width:100%; height:500px; overflow:auto;">
        <asp:CheckBoxList ID="chkNotify" 
            runat="server" 
            CssClass="checkboxlist_nowrap"
            RepeatLayout="Table" 
            /> 
    </div>
    <asp:Button ID="btnSaveNotifications"
        runat="server" 
        Text="Ok"
        />
</div>

The popup displays correctly however the labels for each checkbox are on the line below the checkbox. I cant seem to figure out why this happens, at first I assumed that the div containing the CheckBoxList was simply too small so I gave each div a fixed width, but that didn't help anything.

I have also tried applying this CSS

.checkboxlist_nowrap tr td label
{
    white-space:nowrap;
    overflow:hidden;
    width:100%;
}

It didnt help but im unsure about if the stylesheet actually was used even though I have:

  <link href="../css/HelpDesk.css" rel="stylesheet" type="text/css" />

in my head tags.

Can anyone suggest anything else I can try?

Thanks

UPDATE: Here is my Jquery:

 $(function () {
    $("#dialog").dialog({
       autoOpen: false,
       show: "blind",
       width: 400, 

       hide: "explode"
    });

And here is how I populate the CheckBoxList:

 Private Sub populateCheckBoxList()

      Dim notificationList As DataTable
      notificationList = dbGetNotificationsList(1)

      For Each dr As DataRow In notificationList.Rows

         Dim li As New ListItem
         li.Text = dr("FullName")
         li.Value = dr("ID")

         If (dr("Checked") = 1) Then
            li.Selected = True
         Else
            li.Selected = False
         End If
         chkNotify.Items.Add(li)

      Next

   End Sub

I have tried moving the CheckBoxList to just inside the form tag so that no other styles can be applied and nothing should affect it however I still get the same issue.

like image 523
Purplegoldfish Avatar asked Nov 01 '11 14:11

Purplegoldfish


People also ask

How can I get checkbox and label on same line?

Method 1: By making the position of checkbox relative, set the vertical-align to the middle can align the checkboxes and their labels. Here, we have made the position of the checkbox relative to the label. So the checkboxes are aligned according to the label.

How do I change the position of a checkbox in HTML?

Use the :checked pseudo-class, which helps to see when the checkbox is checked. Style the label with the width, height, background, margin, and border-radius properties. Set the position to "relative". Style the "checkbox-example" class by setting the display to "block" and specifying the width and height properties.

What is checkbox control in asp net?

It is used to get multiple inputs from the user. It allows user to select choices from the set of choices. It takes user input in yes or no format. It is useful when we want multiple choices from the user. To create CheckBox we can drag it from the toolbox in visual studio.


4 Answers

For me none of the above solutions worked and so i looked up the exact HTML rendered and found that the label had the display property set to block. Changing it to inline worked:

.checkboxlist_nowrap label
{
     display:inline;
}
like image 152
Sarvesh Gupta Avatar answered Oct 16 '22 11:10

Sarvesh Gupta


I'm thinking it's a CSS problem... I couldn't reproduce the whitespace wrapping with what you posted. You might want to make sure the width of your dialog is set correctly in jQuery.

Something like:

$("#dialog").dialog({
    modal: true,
    autoOpen: false,
    draggable: false,
    resizable: false,
    width: 400,
    buttons: {
        Update: function () {
            $(this).dialog('close');
        },
        Cancel: function () {
            $(this).dialog('close');
        }
    }
});

Also, a really great way to check CSS (and javascript) is using Google Chrome's Dev Tools. They're packaged with Chrome. All you have to do is right-click on the element you're having trouble with and hover over the HTML in the window. It'll tell you all the classes being applied to it and will show you the margins/width/everything. It has been infinitely helpful for me.

like image 29
tedski Avatar answered Oct 16 '22 12:10

tedski


I was having a similar problem. Found an answer on another stack overflow article which I have pasted below.

You want to have display:inline applied to the element that ASP generates to hold the label text, not the control itself. So, for example:

<style type="text/css">
label { display: inline-block; }
</style>
<asp:CheckBox Text="This text appears on same line as checkbox" runat="server" />
like image 2
John Avatar answered Oct 16 '22 11:10

John


Using an ASP.NET checkbox control, I had the same problem with an unwanted linefeed between a checkbox and its label. I believe the problem reared its ugly head because this section of code was wrapped in a class that applied a {display: block} style. I solved the problem by first adding a CssClass attribute to the checkbox:

<asp:Repeater
    ID="UserRoleList"
    runat="server">
    <Itemtemplate>
        <asp:CheckBox
            ID="RoleCheckBox"
            CssClass="sameLine"
            AutoPostBack="true"
            Text='<%# Container.DataItem %>'
            runat="server" />
        <br />
    </ItemTemplate>
</asp:Repeater>

Looking at the rendered html in the browser by viewing the source I saw that the style added a span and that the asp.net checkbox control was rendered within the span as an input tag and a label. I tried applying my style to just the span alone but it didn't work nor did applying the style to the input tag. What worked for me was applying the style to the label:

.sameLine label {
    display: inline;
}
like image 1
Steven Gregory Avatar answered Oct 16 '22 10:10

Steven Gregory