locked
CheckBoxList in gridview header template RRS feed

  • Question

  • User-153404742 posted

    Hi,

    I have an ItemTemplate in the gridview templatefield inside which I have a checkboxList that is dynamically created.  When I bind it, I actually am setting the DataTextField and DataValueField as I need to access these in code behind in rowdatabound.  However, I dont' want to display the actual text and value field  and just display the checkbox by itself...how do I do this?

      <ItemTemplate>
                                    <asp:CheckBoxList DataTextField="IDText" DataValueField="IDVal" ID="cblRolesHeader" runat="server" RepeatDirection="Horizontal" CellSpacing="2" CellPadding="2"></asp:CheckBoxList>
                                </ItemTemplate>

    Friday, May 7, 2021 5:02 PM

Answers

  • User409696431 posted

    Did you try my solution?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 11, 2021 12:30 AM

All replies

  • User409696431 posted

     I am assuming, since you are using a CheckBoxList, not a CheckBox, that there will be multiple boxes displayed. Can you explain how people will know what they are checking if there is no text beside it? 

    (I'm trying to figure out the problem you are trying to solve.)

    At any rate you can make the label for the checkbox not visible.  If you look in the source of the rendered page, you'll find the checkbox list rendered as a table.  Target the table's ID with CSS like the following to make the labels it contains in its cells not visible:

    table#MainContent_CheckBoxList1 tbody tr td label
    {
        visibility:hidden;
    }

    Saturday, May 8, 2021 4:06 AM
  • User-939850651 posted

    Hi inkaln,

    However, I dont' want to display the actual text and value field  and just display the checkbox by itself...how do I do this?

    If you just don't want to display the text, you can add an empty column to the data source, and then set the DataTextField to change the column. like this:

    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[] {
                        new DataColumn("Text"),
                        new DataColumn("Value"),
                        new DataColumn("emptyCol")
                    });
    
                    dt.Rows.Add("Text1","Value1","");
                    dt.Rows.Add("Text2","Value2","");
                    dt.Rows.Add("Text3","Value3","");
                    List1.DataSource = dt;
                    List1.DataTextField = "emptyCol";
                    List1.DataValueField = "Value";
                    List1.DataBind();

    Hope this can help.

    Best regards,

    Xudong Peng

    Monday, May 10, 2021 9:04 AM
  • User-153404742 posted

    but what if I want to use the dataTextField value in code behind when traversing through each cbl items?

    Monday, May 10, 2021 5:58 PM
  • User409696431 posted

    Did you try my solution?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 11, 2021 12:30 AM
  • User-939850651 posted

    Hi inkaln,

    Using css style can achieve the effect you mentioned, but you can still find the value of Text from page elements. like this:

    If this makes no difference to the results you expect, then using CSS styles is the easiest. Otherwise, you have to bind the Text value to empty column data and perform relatively troublesome data processing.

    By the way, using visibility: hidden will still keep the space occupied by the elements on the page,but if you don't want this, you could use attribute display: none . Of course it's up to you.

    Best regards,

    Xudong Peng

    Tuesday, May 11, 2021 2:51 AM