How can I display two columns in the DropDownList in asp.net?
SqlDataSourceNamePeop.SelectCommand = "select name,forename from people";
DropDownListCaptain.DataTextField = "name";
Like this it displays only the names. But how can i display the names+forenames?
<asp:DropDownList ID="DropDownListPeople"
runat="server"
DataSourceID="SqlDataSourceNamePeop">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceNamePeop"
ConnectionString="xxxxx"
runat="server">
</asp:SqlDataSource>
The easiest way is to make your sql query like this:
SqlDataSourceNameJuc.SelectCommand = "select name + ' ' +forename as FullName from people";
then bind this custom property to your drop down list like this:
DropDownListCaptain.DataTextField = "FullName";
Let me know if it fixed your problem, thanks.
Change your select command to
"Select name + ',' + forename as 'name' from people"
<asp:DropDownList ID="DropDownList9" runat="server"
DataSourceID="SqlDataSource3" DataTextField="**CALL_NAMESURNAME**"
DataValueField="CALL_NAME">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:McCarthyConnectionString %>"
SelectCommand="SELECT [CALL_NAME], [SURNAME],**CALL_NAME + SURNAME AS [CALL_NAMESURNAME]** FROM [Sheet1$] where COY_NAME LIKE '%abc%'">
</asp:SqlDataSource>
CHECK THE ** ** PARTS
IList<PM_ClientInformation> objPM_ClientInformation = new List<PM_ClientInformation>();
//get information in objPM_ClientInformation
foreach (PM_ClientInformation f in objPM_ClientInformation)
{
string text = string.Format("{0}{1}",
f.PM_cliFirstName.PadRight(10,'\u00A0'),
f.PM_cliLastName.PadRight(10,'\u00A0'));
ddlClientName.Items.Add(new ListItem (text ,f.PM_cliId.ToString()));
}
it defiantly work.
Dropdownlist cannot display two items. you have to merge trhem and then bind the result to it.
note that there are some other dropdownlist (ajax based or simple ) which are able to bind to two or more columns, but ASP.net is not able to cope with it
Related
I need to auto select an item from dropdownlist when page is loaded.
.aspx code:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="fullname" DataValueField="fullname">
<asp:ListItem>Any</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PHSNew %>" SelectCommand="SELECT DISTINCT [fullname] FROM [web_Users] WHERE ([role] = #role) ORDER BY [fullname]">
<SelectParameters>
<asp:Parameter DefaultValue="md" Name="role" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Now for example if dropdownlist has 4 items (John, Albert, Epstien and Any).
in .cs file I run sql query to get a string name, from database which should be selected in this dropdown.
I just used DropDownList1.selectedValue= name. It worked
But now the problem is the data in database keeps on changing. I may get string name = "Renzo". Renzo WAS part of the database but now it's removed. In that case I have to select "Any".
I tried executing this code:
string s = "Albert";
ListItemCollection li = DropDownList1.Items;
foreach (ListItem l in li)
{
string s1 = l.ToString();
if (l.ToString() == s)
{
DropDownList1.SelectedValue = s;
return;
}
else
{
DropDownList1.SelectedValue = "Any";
}
}
And I am calling this code in Page_Load.
But I am able to get only one item from DropDownlist1 and that is "Any".
How can I get all items and auto select a particular item from dropdownlist?
I believe the issue with your code is that you are using l.ToString(). You would want to use l.Value instead in your comparison.
Another way of doing this you can use the find methods of the item collection.
string s = "Albert";
ListItem li = DropDownList1.Items.FindByValue(s);
if (li != null)
{
li.Selected = true;
}
else
{
DropDownList1.SelectedValue = "Any";
}
I want to populate a dropdown list with values from a table I created. I only want to populate the list with one of the fields- the languages in my table. I think I have connected to the data source correctly, but I don't know what I have to do to get the values into the list. I can enter my own values but I'd rather have this automated.
This is what I have so far, but I'm guessing there's more to it than just linking the list to the data source.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HBshareIndexConnectionString %>"
SelectCommand="SELECT * FROM [Web_Metrics] WHERE ([LCID] = #LCID)">
<SelectParameters>
<asp:QueryStringParameter Name="LCID" QueryStringField="LCID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text="Select LCID: " ></asp:Label>
<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1" DataTextField="LCID" DataValueField="LCID">
<asp:ListItem>Select LCID...</asp:ListItem>
</asp:DropDownList>
Thanks for the help. I got the dropdown list populated now, but I was wondering how do I actually get the repeater I'm using to display the details of the LCID the person selects? I've seen people talking about page.isPostback but I don't know what that is or if it works with my current setup. I need to somehow get the LCID they selected and then refresh the page to show the details of that LCID. Does anyone have any ideas?
Your problem is that you're trying to define list items and a data source.
If you want to insert a "Select an item.." option, I would suggest prepending it to your resultset (getting it to always be first with a UNION and ORDER BY could be difficult depending on your fields) or inserting it after databinding in your code behind:
Modification to DropDownList1s attributes:
<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1" DataTextField="CountryName" DataValueField="LCID" OnDataBound="InsertChooseItem" />
C#:
protected void InsertChooseItem(object sender, EventArgs e)
{
ListItem selectOnePlease = new ListItem("Select LCID..", 0);
DropDownList1.Items.Insert(0, selectOnePlease);
}
VB:
Protected Sub InsertChooseItem(sender As Object, e As EventArgs)
Dim selectOnePlease As New ListItem("Select LCID..", 0)
DropDownList1.Items.Insert(0, selectOnePlease)
End Sub
You've specified the select parameter to be a query string, so the data in your DropDownList will only be populated when the URL resembles something like:
http://{Your server name}/Default.aspx?LCID=1
That doesn't make any sense though because the LCID column in your table should be unique, so although this will work there will only be one value in the drop down list.
I think what you want is to display all the languages from the database in the drop down, here's an example:
<asp:SqlDataSource ID="sqlDS" runat="server"
ConnectionString="<%$ ConnectionStrings:HBshareIndexConnectionString %>"
SelectCommand="SELECT LCID,Language FROM [Web_Metrics]">
</asp:SqlDataSource>
<asp:DropDownList ID="ddlLanguages" AppendDataBoundItems="true" Width="150px" runat="server" DataSourceID="sqlDS" DataTextField="Language" DataValueField="LCID">
<asp:ListItem>Select Language</asp:ListItem>
</asp:DropDownList>
Just a note, you should never display the ID to the client, it's totally meaningless to them, the ids are mostly used by developers in the background that's why in the drop down I set the:
DataTextField="Language" (This is the language name visible to the user)
DataValueField="LCID" (Not visible to the user, but useful for any additional processing in code behind)
AppendDataBoundItems="true" - this line of code will keep all the items you've manually added to the drop down, e.g "Select Language" and will append any data bound items e.g from a SQL table
I made form for inserting values into DB, and one column is CompanyID, and there is table Companies where that CompanyID is primary key.
How to get like dropdown menu or something, so users don't type like 1213 (ID), but they select it from list, or something like that?
Thanks.
Following is the simplest method.
Create & configure a SqlDataSource.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT [CompanyID], [CompanyName] FROM [tbl_Company] ORDER BY [CompanyName]">
</asp:SqlDataSource>
Drag and drop a dropdown list from the toolbox. set datasourceid, datatext field and data value field.
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="CompanyName" DataValueField = "CompanyID"
DataMember="DefaultView" DataSourceID="SqlDataSource1">
</asp:DropDownList>
Run the page.
Select company name and ID from company table in array.
then with that array create your drop down list as follows
SQl = "select id, name from company";
echo '<select>'
foreach($companies as $company)
echo "<option value='$company[id]'>$company['name']</option>";
echo '</select>'
I need to make a dropdownlist combining two fields from an xml file...
XML file:
<?xml version="1.0" encoding="utf-8" ?>
<exchangeRates>
<rate country="aud" >0.97</rate>
<rate country="usd" >1.01</rate>
</exchangeRates>
I need for the dropdownlist to show country and the rate like
aud 0.97
usd 1.01
Something like
SELECT country + rate AS NewColumn FROM XML above
And then use NewColumn for the DataValueFIeld:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDBSrc" DataTextField="NAME" DataValueField="NewColumn"
>
<asp:XmlDataSource ID="ListItems" runat="server" DataFile="~/ExchangeRates.xml">
</asp:XmlDataSource>
How do I do this correctly in asp.net?
You could probably use LINQ to XML.
Something like (not tested)
dim selectList = (from x in xmldoc
select new SelectListItem { Text = x.Attribute("country") + ' '+ x.Value, Value = x.Id });
You could ad a pre-render event to the control and modify the control there
<asp:DropDownList ID="DropDownList1" runat="server" onprerender="DropDownList1_PreRender"
DataSourceID="SqlDBSrc" DataTextField="Country" DataValueField="Value"
>
Note: adjust the DataText and DataValue Fields to populate the unmodified data.
In your code behind, this modifies the :
protected void DropDownList1_PreRender(object sender, EventArgs e)
{
foreach (ListItem item in DropDownList1.Items)
{
item.Text = item.Text + " " + item.Value;
}
}
This is one way to do it but there may be other better ways before binding the list.
<asp:DropDownList ID="DropDownList9" runat="server"
DataSourceID="SqlDataSource3" DataTextField="**CALL_NAMESURNAME**"
DataValueField="CALL_NAME">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:McCarthyConnectionString %>"
SelectCommand="SELECT [CALL_NAME], [SURNAME],**CALL_NAME + SURNAME AS [CALL_NAMESURNAME]** FROM [Sheet1$] where COY_NAME LIKE '%abc%'">
</asp:SqlDataSource>
Check the ** ** parts.
I have two dropdownlists for a search, the 1st list is for the city and the second is for the area within the selected city. I would like to add a default value in the 2nd dropdownlist that will search ALL of the areas by default unless a specific area is selected from the list that contains the areaID for a specific search.
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="cityName" AutoPostBack="true" DataValueField="cityID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Cities]"></asp:SqlDataSource>
<asp:DropDownList ID="DropArea" runat="server" DataSourceID="SqlArea"
DataTextField="areaName" DataValueField="areaID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlArea" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [area] WHERE ([cityID] = #cityID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="0" Name="cityID"
PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
Bind the second from code behind. Create a data table and insert the Select text at the begining and bind the datatable to the DDL.
DataTable dt =
DataRow dr = dt.NewRow()
dr["areaName"] = "All";
dr["SqlArea"] = "All";
tbldata.Rows.InsertAt(dr,0);
DropArea.DataSource = dt;
DropArea.DataBind();
You can try to add for second dropdownlist OnDataBound="DropArea_DataBound" event.
And use in code:
protected void DropArea_DataBound(object sender, EventArgs e)
{
DropArea.Items.Insert(0, new ListItem() {Value = "-1", Text = "ALL"});
}
And then when you handle click event, check:
var value = DropArea.SelectedItem.Value;
if(string.equals(value, '-1')
{
use your logic here
}
Hope it will help with your problem.
Best regards, Dima.
Chris,
I got the idea below from an answer HERE, but basically you want to modify your query to be in the form of the following:
Your Original:
SELECT * FROM [Cities]
Change To:
SELECT City FROM [Cities]
UNION ALL
SELECT 'ALL'
Old question, but you never found an answer.