How to add the checkbox item at the new line? - c#

I have a checkbox list like below:
<asp:CheckBoxList id="chx" runat="server" repeatdirection="Horizontal"> <asp:ListItem value="OPTION1">Option 1</asp:ListItem>
<asp:ListItem value="OPTION2">Option 2</asp:ListItem>
<asp:ListItem value="OPTION3">Option 3</asp:ListItem><
asp:ListItem value="OPTION4">Option 4</asp:ListItem>
</asp:CheckBoxList>
And I am dynamically adding the list items to this checkbox using the following C# code:
chx.Items.Clear();
while(dr.Read()){
if(dr.GetOracleString(0).ToString()!="Null"){
chx.Items.Add(dr.GetOracleString(0).ToString());
}
}
My requirement is whenever the new item is added in the checkboxlist, it should check whether the new item is going beyond the border(fixed width) of the page and if so the new item should go the next line of checkboxlist without overlapping below control.
Please help me to fix this.

You can use the property RepeatColumns
RepeatColumns="3"

Related

Is it possible to put a check box inside a DropDownList that uses DataSourceID?

I am trying to put a check box inside a DropDownList, but all I find is examples that the DropDownList uses ListItem, something like:
<asp:DropDownList>
<asp:ListItem/>
</asp:DropDownList>
But the code I'm using doesn't have the ListItem:
<asp:DropDownList DataSourceID="example"> </asp:DropDownList>
<asp:SqlDataSource> </asp:SqlDataSource>
Now how can I make the DropDownList appear with Check Boxes?
The drop down list is not available for multi select. What I suggest is to use a asp:ListBox with SelectionMode="Multiple" from what you get on page convert it to a drop down multi select list.
I suggest asp:ListBox because you can use your DataSource.
so for example if you have
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Width="200px">
<asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Option 3" Value="3"></asp:ListItem>
<asp:ListItem Text="Option 4" Value="4"></asp:ListItem>
</asp:ListBox>
and use a plugin like Chosen, or the Bootstrap Multiselect you can have what you search for.
The key here is to use the ListBox with multiselect.
Short of writing your own dropdown the closest you will probably come is to include an emoji at the start of each option text. Then toggle this from say a ⭕ to a 🔴 when selected

Auto Select one item on Dropdownlist asp.net c#

I have dropdownlist with list item of Yes and No. I want "No" will be displayed on the page but the Selected="true" on the ListItem is not working.
<asp:DropDownList ID="ddlIsDistributor" runat="server">
<asp:ListItem Value="1" Text="Yes">Yes</asp:ListItem>
<asp:ListItem Value="0" Text="No" Selected="True">No</asp:ListItem>
</asp:DropDownList>
Invoke this bit of code in your page load event so it selects the desired value from the dropdown when loading: ddlIsDistributor.SelectedIndex = 0;

asp dropdown reload page when setting dropdown.selectedvalue

I have the following asp dropdown:
<asp:DropDownList ID="ArticleCategories" runat="server" AppendDataBoundItems="true" CssClass="ArticleCategoriesDropDown" onselectedindexchanged="mCategoryItemSelected" AutoPostBack="True">
<asp:ListItem Text="Select Category" Value="" />
</asp:DropDownList>
Once a selection has been made, and the data loaded, I want to set the value in the dropdown to display the value chosen by the user.
I am doing this by:
ArticleCategories.SelectedValue = CategoryID.ToString();
This works fine the first time, but from then the page is stuck on that selection, as the selected value gets loaded before the new value can load.
I have tried disabling the itemchangelistener by:
ArticleCategories.SelectedIndexChanged += new EventHandler(doNothing);
But that does not work.
I have also tried to disable postback which also does not work.
Any ideas?
You should only set the selection when loading the page the first time. In ASP.NET, you can achieve this by checking the IsPostBack property:
if(!IsPostBack)
{
ArticleCategories.SelectedValue = CategoryID.ToString();
}
You can do Autopostback= false
<asp:DropDownList ID="ArticleCategories" runat="server" AppendDataBoundItems="true" CssClass="ArticleCategoriesDropDown" onselectedindexchanged="mCategoryItemSelected" AutoPostBack="False">
<asp:ListItem Text="Select Category" Value="" />
</asp:DropDownList>
Hey before selecting another item from Dropdown you should clear previous selection then selected another one.
ArticleCategories.ClearSelection();
Hope it helps you.

Aspx dropdownlist not displaying top value

I have an aspx page (C# code page). I have some hard coded dropdownlists and for some reason they are not displaying the top list item (value). I added an extra top list item (value) and now the correct values display for the values already in it, but that extra one does not display.
The only functionality I do with my dropdownlists in my C# code is to hide or show them. And then do validation or not based on the selected value.
My aspx code:
<asp:DropDownList ID="ddlAction" runat="server" Visible="True"
AppendDataBoundItems="true" Height="25px" Width="149px">
<asp:ListItem Value="Select">Please Select</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
C# Code:
ddlAction.Visible = false;
ddlAction.Visible = true;
I use dropdownlist's regularly and have never had this problem before. Does anyone have any ideas what the issue could be?
UPDATE TO THIS ISSUE:
I added my items in my C# code as per Rahul. Did a quick test and it worked.
Now this morning, I am once again getting blanks for the first item ("Please Select").
Aspx code:
<asp:DropDownList ID="ddlAction" runat="server"
AppendDataBoundItems="True" Height="27px" Width="159px">
</asp:DropDownList>
C# code:
ddlAction.Visible = true;
ddlAction.AppendDataBoundItems = true;
ddlAction.Items.Insert(0, new ListItem("Please Select","Select"));
ddlAction.Items.Insert(1, new ListItem("Yes", "Yes"));
ddlAction.Items.Insert(2, new ListItem("No", "No"));
ddlAction.DataBind();
Rendered source code:
<select name="ctl00$ContentPlaceHolder1$ddlAction" id="ContentPlaceHolder1_ddlAction" style="height:27px;width:159px;">
<option selected="selected" value="Select"></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
Try to use AppendDataBoundItems = true property of DropSownList into your .aspx page.
you may also assign value from code behind as well like
ddlAction.Items.Insert(0, new ListItem(String.Empty, String.Empty));
use AppendDataBound = true in your aspx coe.
<asp:DropDownList ID="ddlAction" AppendDataBound = true runat="server" Visible="True" Height="25px"
Width="149px">
<asp:ListItem Value="Select">Please Select</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
Edit 1
More detail about List Item
<asp:ListItem Value="-2" Text="Please Select"></asp:ListItem>
<asp:ListItem Value="0" Text="Yes"></asp:ListItem>
<asp:ListItem Value="-1" Text="No"></asp:ListItem>
I suggest you declare your DropDownList ListItems using its internal properties and defining what ListItem must be the selected one:
<asp:DropDownList ID="ddlAction" runat="server" Visible="True" AppendDataBoundItems="true" Height="25px" Width="149px">
<asp:ListItem Text="Please Select" Value="Select" Selected="True"></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"</asp:ListItem>
</asp:DropDownList>
It's the way ASP.NET uses to work and will return you the right selected value on the server side on postbacks.
I think that you don't have to use nor the DataBind() method neither set the AppendDataBoundItems, because, you already inserted the ListItems and you aren't loading options from a database!
I think you need to tell what ListItemIndex is the selected one by seting a value to the DropDownList.SelectedIndex property.
EDIT
Also, try to read to MSDN documentation about the AppendDataBoundItems property and the enter link description here method.

DropDownList doesn't call SelectedIndexChanged?

I have 7 items in my dropdown list like
<asp:DropDownList ID="DdlSortBy" runat="server" OnSelectedIndexChanged="DdlSortBy_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem Value="0">Case 1</asp:ListItem>
<asp:ListItem Value="1">Case 2</asp:ListItem>
<asp:ListItem Value="2">Case 3</asp:ListItem>
<asp:ListItem Value="3">Case 4</asp:ListItem>
<asp:ListItem Value="4">Case 5</asp:ListItem>
<asp:ListItem Value="5">Case 6</asp:ListItem>
<asp:ListItem Value="6">Case 7</asp:ListItem>
</asp:DropDownList>
all items except Case 1 value 0 initiate selected index change event.
Any idea how to fix it?
If it is working for one then it should be working for each of them; an instance in which it won't postback upon selection would be if that item was already selected, say, by default - then you would need to select something else, then re-select said "default" value.
Otherwise, I can't see that any single item would be discriminated against.
The reason might be that the first item is selected by default. What you could try is to add a new item and set it to be the first:
<asp:ListItem Value="-1">please select</asp:ListItem>
That way, when you select Case 1, it will fire the event.

Categories

Resources