Reputation: 333
So what I want to do is when you click the dropdown it shows two items from the dropdownlist. Left side will show the ID and the right side will show the text.
So will look like (98739 | Smoker) then when you click onto the item you want the dropdown list will only show "Smoker"
I have tried
string sqlGetClass = "select [code], [SnoMedCode] + ' | ' + [Descrip] as [fullDescrip] from LT__SmokingStatus";
SqlCommand cmdSmoke = new SqlCommand(sqlGetClass, con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmdSmoke);
DataSet ds = new DataSet();
da.Fill(ds);
ddlSmokeStat.DataSource = ds;
ddlSmokeStat.DataTextField = "fullDescrip";
ddlSmokeStat.DataValueField = "Code";
ddlSmokeStat.DataBind();
con.Close();
That works fine and it shows (98739 | Smoker) in the dropdown. But was wondering anyway of hiding the 98739 code from there after I have selected the item. I know I am setting it and calling that. So any other way to combine them to do as I wish?
Upvotes: 0
Views: 262
Reputation: 7918
First, I've made some editing to the title to clarify the task (you are not hiding other items in the list, but just a part of selected item).
Based on that assumption, the most straightforward solution would be re-query the same control with modified select SQL statement like: "select [code], [Descrip] from LT__SmokingStatus"
and correspondingly modify the DataTextField
binding like: ddlSmokeStat.DataTextField = "Descrip";
Also, you need to persist the Selected item between post-backs (for example, in Session Variable) and then show it selected after binding. Otherwise, you can implement partial/conditional web page rendering via Microsoft AJAX (use UpdatePanel and specify Dropdown Trigger events).
There are more efficient solutions (for example, to store the content of data fields in a List or Array, or Dictionary and bind Dropdown control to that data structure, as described in the article: Binding-ASP-NET-DropDownList), but it requires substantial code modification.
Also, this task can be accomplished by using Javascript code snippets on HTML Select element rendered by ASP.NET Drop-down control, but that solution won't work if Javascript is disabled on the client's computer.
Hope this may help. Best regards,
Upvotes: 1
Reputation: 11480
I would personally do this via JavaScript, a jQuery sample:
$('#<%= Sample.ClientId %>').blur(function () {
var content = $('#<%= Sample.ClientId %> > option:selected').text().split('|');
$('#<%= Sample.ClientId %> > option:selected').text(content)
});
This would be a simple approach, otherwise you'll need to anchor onto a Postback. Then wrap in an Update Panel or do some form of Ajax to handle via Server Side.
Upvotes: 1