Pre select records in multi select listbox using FindbyText

Hi Everyone,

I am having problems in preselecting the multiselect listbox depending on the value in database. I have listbox for displaying all the languages and want them to preselect values from database before updating them.

Database field: language stores the value along with delimiters like French;Dutch;Spanish. I would like to separate the delimiters and pre-select the values in listbox (lbLanguage). I am using datareader.

Please let me know what is wrong in the following code.

My Code:

string langList=dreader[19].ToString();

string[] langArray=langList.Split(';');

for(int i=0;i<langArray.Length;i++)

{

string oneLang=langArray[0].Trim().TrimEnd();

ListItem li=lbLanguage.Items.FindByText(oneLang.ToString().TrimEnd().Trim());

if(li!=null)

{

li.Selected=true;

}

}

Thanks a lot in Advance.

Regards

Shini

 

 

 

 

 

 

 

 

-1
Shini
1/20/2006 12:16:22 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

3 Replies
1531 Views

Similar Articles

[PageSpeed] 28

I just tested this and it works:

In the aspx file, inside of the <form> tags:
<asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple">
 <asp:ListItem Value="1">1</asp:ListItem>
 <asp:ListItem Value="2">2</asp:ListItem>
 <asp:ListItem Value="3">3</asp:ListItem>
 <asp:ListItem Value="4">4</asp:ListItem>
 <asp:ListItem Value="5">5</asp:ListItem>
 <asp:ListItem Value="6">6</asp:ListItem>
</asp:ListBox>

That add's this to the aspx.cs file:
protected System.Web.UI.WebControls.ListBox ListBox1;

Then to initially set the selected values (probably in Page_Load, !IsPostBack):
string dbValueList = "1;2;5";

string [] dbValueArray = dbValueList.Split(';');

for (int i=0; i<dbValueArray.Length; i++)
{
 string oneValue = dbValueArray[i].Trim();

 ListItem listItem = this.ListBox1.Items.FindByText(oneValue);
 if ( listItem != null )
  listItem.Selected = true;
}

NC...

0
NC01
1/20/2006 3:29:55 PM

Hi NC01

Thanks very  much  for your time in solving my query. Mine is working now, I just resolved it after posting the question.

Solution:

I was trimming all the white spaces at the end of each language while storing in the Array. Then I found out that my database field was stored with spaces at the end.. So removing the Trim() in my code worked.

string langList=dreader[19].ToString();

string[] langArray=langList.Split(';');

for(int i=0;i<langArray.Length;i++)

{

string oneLang=langArray[i];

ListItem li= lbLanguage.Items.FindByText(oneLang.ToString());

if(li!=null)

{

li.Selected=true;

}

}

Thanks  again.

Regards

Shini

 

 

 

0
Shini
1/26/2006 4:49:03 PM

Ahaaaa! When having troubles like that it's always best to open the page in the browser and do a View Source to physically examine the element values.

 

NC...

 

0
NC01
1/26/2006 6:52:10 PM
Reply:

Similar Artilces:

How to use Select case statement to select web form controls
Hi; I don't know why I can't conceptionalize  this but I want to take these if statements and put them in a select case. I am testing for multiple selection from several DDLs on my web form.   If DropDownList1.SelectedIndex > -1 And DropDownList2.SelectedIndex > 0 Then If Me.CheckBox1.Checked Then ObjectDataSource1.FilterExpression = "assignee = {0} AND assignor = {1}" Else ObjectDataSource5.FilterExpression = "assignee = {0} AND assignor = {1}" End If End If If DropDownList1.SelectedIndex > -1 And DropDownList3.SelectedIndex >...

Using selected values from a Multi Select Listbox in a SQL query
I have seen a few post concerning this but don't really understand fully as I am new to this.  I would appreciate any help 1) First I loop through the Listbox control and get the selected values: Dim li As ListItemFor Each li In lbPClass.Items If li.Selected = True Then strPC += li.Value & ", " End If Next Label5.Text = strPC The resulting string looks something like: 101, 102, 100 ,    I get rid of the last space and "," with the following statement Label6.Text = Left(Label5.Text, Trim(Len(Label5.Text) - 2)) 2) I would then like to ...

Multi-select ListBox and Select All
Hi,I'm having an issue with some JS code for IE6 (no need for x-browser) and multi-select listbox.I'm trying to implement a Select All button.Current code is : function SelectAll(obj){ for (var i = 0; i < obj.options.length; i++) { obj.options[i].selected = true; } } which logically seems to work.  The problem I have is that this does not seem to work until I click the multi-select again.  It seems that although the options are marked as selected, the screen does not update to reflect this until something triggers it (in this case trying to click in ...

select statment to select records by using and
i am trying to retrive records by using 'and' but i get error when i run the sql. when i keep single record i get data but when i try more then one i get error. this is the sql statment select a.ReportNumberfrom  dbo.UC_RptColoredGem awhere  a.ReportNumber = '2235194001' and '2235200001' and '2235203001'  this is the error Server: Msg 156, Level 15, State 1, Line 3Incorrect syntax near the keyword 'and'. i have to retrive a list of records. -------------------------Anything is possible. If Post.Solved.Problem Then &quo...

SELECT from SELECT
Hi! I remember seeing an example where you can do a select from a nested select, but I cannot seem to make it work. So it would be something like this: select max( name ) from ( select name from customer union select name from vendor ) Was I dreaming I saw that? Thanks! D In article <4500243f@forums-2-dub>, daniel_AT_infrontsoftware_DOT_com@N0SPAM.com says... > Hi! > > I remember seeing an example where you can do a select from a nested > select, but I cannot seem to make it work. No, you weren't. Try (UNTESTED!) adding a...

Problem With Using Multi Select Listboxes and Callbacks
I have several multi-select listboxes on my webpage. These listboxes work fine when I submit the form with a postback. My problem is that when I do a Client Callback to the server, the multiple selections are lost and it only recognizes the first selection.For example, say I have a listbox like this :<asp:ListBox ID="Test" runat="server" Rows="3" SelectionMode="Multiple">     <asp:ListItem Text="A" Value="A"/>     <asp:ListItem Text="B" Value="B"/>     <asp:ListItem Text="C" Value="C"/></...

Forms , Select and option selected.
Name: Jirrick Email: jurahudecatgmaildotcom Product: Firefox Release Candidate Summary: Forms , Select and option selected. Comments: I've found problem with SELECTED option in <select> forms. I wanted to use pre-selected option in my form , but when I used the Firefox 3 RC1 to render it there wasn't any preselected item. I tried to rewrite code ( use selected="selected" ) , but still no effect. I was confused and tried to use others viewers ( IE 8 , OP 9 , Konqueror ) and the pre-selected item worked well in these browsers. The bug has to be in FF 3 ...

Multiple-Select Listbox only returns first selected value (and I'm not using SelectedItem...)
I'm looping through the items of a listbox, as you can see I'm raising a custom event, passing the selected values with custom event args up...  What happens however, is that only the first item will be tagged as being selected, no matter how many listbox items I select with control. Ie If I select "Transducer, Pressure Transmitter, Pressure & Temperature" the following is returned: Transducer is selected;Pressure transmitter is not selected;Pressure & Temperature is not selected; I encoded the ampersand & so do not worry about string concat. This is t...

Using a Multi-select Listbox as an SQL parameter for a GridView
Hi All,I'm confused. Using VWD2005 with VB.NET. Using a multiple select Listbox as a paramater in the SQL statement that populates the gridview.First of all I'm surprised that since the gridviews sqldatasource wizard lets you step through all the way to selecting a listbox control as a parameter in a SQL statement, Surely the wizard should automatically detect that the selected listbox is multi-select and asp should generate the code to handle multiple selected items? Anyway, I've read various posts and cannot establish what the correct ASP.NET solution is? This is ...

Using a multi select Listbox for datasource parameter of gridview
Hi All,I'm confused. Using VWD2005 with VB.NET. Using a multiple select Listbox as a paramater in the SQL statement that populates the gridview.First of all I'm surprised that since the gridviews sqldatasource wizard lets you step through all the way to selecting a listbox control as a parameter in a SQL statement, Surely the wizard should automatically detect that the selected listbox is multi-select and asp should generate the code to handle multiple selected items? Anyway, I've read various posts and cannot establish what the correct ASP.NET solution is? This is what I cu...

Pre-select dropdownlist using FindByText
Hello Everyone, Can anyone help me please?? In my c# webpage, I want my dropdownlist Sedation to display either Yes/No based on the value from database.  But during runtime it is not pre-selecting the text. Am i doing anything wrong? private void btnPracticeGo_Click(object sender, System.EventArgs e) { sqlConnection1.Open(); System.Data.SqlClient.SqlDataReader dreader1; dreader1 = PracticesByIdCommand1.ExecuteReader(CommandBehavior.SingleRow); if (dreader1.Read()) { txtPracticeName.Text= dreader1[1].ToString(); txtPracticeaddr1.Text = dreader1[2].ToString(); tx...

how do I select certain record in sql server? and how do I select record randomly?
how do I select certain record in sql server? like I want to select the record 30how do I select record randomly? Hello, usually in each table in the database there is a primary key or unique key. Usually this key is an indentity, that is increased by a number of your choice. So, can't you select the record where ID = 30 ? Don't you have such a key ?   regardsBilal Hadiar, MCP, MCTS, MCPD, MCTMicrosoft MVP - Telerik MVP To get a random row, you can order by NEWID: SELECT TOP 1 * FROM Tbl ORDER BY NEWID() NEWID produces a pseudo-random GUID -- so ordering by it will give y...

how to select multiple files at a time using Ctrl+select and upload the attachments in C# .Net 1.0?
Hi, Do any one know how to select multiple files at a time using Ctrl+select and upload the attachments in C# .Net 1.0? I know that by placing 4 or 5 attachment controls we can do that.But I want to select multiple at a time. Do any one know this???  i think there is no option to select multiple file at a time ... instead you  can use Upload Multiple files each at one time like Gmail..  Regards,M.VenkatesanMy Blog : http://venkat-dotnetsamples.blogspot.comPlease remember to click “Mark as Answer” on the post that helps you,and to click “Unmark as Answer” if a marked po...

Why is the select saying selected after an item is selected?
This was a whitelisting issue due to a software upgrade at hostgator..  Nothing wrong with my code..Scot King SEO Software Tool | Search Engine Optimization"Get your page rank high"  huh? It is smart to put your question in the body and not just in the title. Have no idea what your question is or if it even a question.Eric Pascarello.com | Twitter epascarello | LinkedIn use thisddl.options[ddl.selectedIndex].selected = false; //Clear the selection ddl.options[1].selected = trueMAKMark as Answer if this reply helps youMVP ASP/ASP.NetASP.Net Hosting : Host DepotMy Sit...