Dropdownlist objectdatasource - asp.net

I have a dropdownlist connected to a objectdatasource. How can I get the DataValueField=TypeId ? I use the onselectedindexchanged to get the selected Type, how can I do the same with TypeId?
<asp:DropDownList ID="DropDownList" runat="server"
DataSourceID="ObjectDataSource" DataTextField="Type"
DataValueField="TypeId" AutoPostBack="true"
onselectedindexchanged="DropDownList_SelectedIndexChanged">
</asp:DropDownList>

To get the value:
int id = int.Parse(DropDownList.SelectedValue);

Not sure if I understand....do you want to get the selected value? In this case, this is what you have to do
string typeId = DropDownList.SelectedValue

Related

Cant set and get value using dropdown list

This dropdown list is inside a gird view:
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:DropDownList ID="Hello" runat="server" SelectedValue='<%# Eval("beta") %>'>
<asp:ListItem Value="" Text="-">-</asp:ListItem>
<asp:ListItem Value="0" Text="0">0</asp:ListItem>
<asp:ListItem Value="1" Text="1">1</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
Every time it has to load the page containing this gridview I get exception:
{"'beta' has a SelectedValue which is invalid because it does not exist in the list of items.\r\nParameter name: value"}
The value of beta is null, however,
<asp:ListItem Value="null" Text="null">null</asp:ListItem>
Doesnt work... Note beta comes from database as the next possible values: null, 0 or 1 .Also tried getting the values from another dropdown list but doesnt work, i tried this:
GridViewRow row = GridView1.Rows[i]; //this is inside a for loop
var ddl = row.Cells[8].Controls[0] as DropDownList;
string test= ddl.SelectedIndex;
I allways get ddl as null when debugging.
So the questions are:
1. How to load the dropdown with 3 values one of them beeing null?
2. How to read the selected dropdown value without using any events like onchange or whatever, example:
CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
bool isChecked = chk.Checked;
To answer you quetsions:
You can't set null as a value for a ListItem, because null is not a string literal. null means that the value is missing. null itself is "not a value". So you will need to handle that in your code:
drpList.SelectedValue = dbObj.FieldValue ?? "(n/a)";
I'm not sure what you are asking here. I would suggest using FindControl instead of using indices to access your controls.
As for your problem, Refer below article for dropdownlist in asp.net
http://www.laptrinhdotnet.com/2015/07/su-dung-control-dropdownlist-trong.html

'ddpJobType' has a SelectedValue which is invalid because it does not exist in the list of items

im trying to bind the data from database to dropdownlist and i want to set the default value as "select". but i got this error.
My Code is:
<asp:DropDownList ID="ddpJobType" runat="server" Width="226px" Height="26px"
onselectedindexchanged="ddpJobType_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="0" Text="--Please Select Type--"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ErrorMessage="Please Select JobType " Font-Size="Small"
ForeColor="Red" ControlToValidate="ddpJobType"
ValidationGroup="VGPJobPost"></asp:RequiredFieldValidator>
If you are banding data using DataSource, then your default item will not be available after the call of DataBind function. You need to add it manually after calling databind something like below
dropDownList.DataSource= yourdataSource;
dropDownList.DataBind();
dropDownList.Items.Insert(0, new ListItem("--Please Select Type--", "0"));
Another option is in the designer you need to specifiy
AppendDataBoundItems=true
This will make sure your designtime list items are present after databind

Need to set 1st value as select in Drop down list when the data source is table in database

<asp:DropDownList ID="ddl_Role" runat="server" DataSourceID="Sql_role"
DataTextField="Role_Name" DataValueField="Role_ID">
</asp:DropDownList>
<asp:SqlDataSource ID="Sql_role" runat="server"
ConnectionString="<%$ ConnectionStrings:Mytime_role %>"
SelectCommand="SELECT * FROM [Role_Alert]"></asp:SqlDataSource>
<asp:RequiredFieldValidator ID="Rfv_role" runat="server" ControlToValidate="ddl_role"
Display="Dynamic" ErrorMessage="*Role is Required field" Font-Italic="True" ForeColor="Red"
InitialValue="0"></asp:RequiredFieldValidator>
it gives only the starting text in database table as first value in Drop down list .so i need select as default one then the remaining list as usual
You set the selectedItem of the dropdownlist after the DataBind method.
dlTest.DataSource = // some datasource;
dlTest.DataBind();
dlTest.Items.FindByValue(search_value).Selected = true;
You can search for the item, either using FindByText or FindByValue. Alternatively, if you could store the selected item text/value, then you can set the dropdownlist selected item in the DataBound event. This will be fired after the data has been binded to dropdownlist.
protected void dlTest_DataBound(object sender, EventArgs e) {
dlTest.Items.FindByValue(search_value).Selected = true;
}
<asp:DropDownList ID="DropDownListID" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="Default text" Value="Default value" />
</asp:DropDownList>
Add a default item and also attribute AppendDataBoundItems="true"

data binding to dropdownlist in asp.net?

For a Label in we bind a data using
<asp:Label ID="Label2" runat="server" Text='<%#Eval("address") %>'></asp:Label>
how to bind data to dropdownlist like that?
asp:DropDownList ID="droplist" runat="server" >
<asp:ListItem Text="admin"></asp:ListItem>
<asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>
Like this....
<asp:DropDownList ID="droplist" runat="server" SelectedValue='<%#Eval("fieldname")%>'>
<asp:ListItem Text="admin"></asp:ListItem>
<asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>
Note that intellisense will not pick SelectedValue out. You will of course need to populate the dropdown with the data... using any method that suits
Either put your datasource in the DropDownList declaration like here: populate dropdownlist
Or use Codebehind like this:
Eg: inside Page_Load():
List<string> ItemsToGoInDropDown = new List<string>{"manager", "admin", "etc"};
droplist.DataSource = ItemsToGoInDropDown;
droplist.DataBind();
Put the data in hidden field. Then asigen that in drop down in Gridview Rowdatabound Event.Like This.
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hf = (HiddenField)e.Row.FindControl("hf");
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
ddl.SelectedValue = hf.Value;
}

Dropdownlist bind with parameterized datasource don't keep value after postback

There is a drop down list bind with datasource -> Called This DropDownList A
This datasource get another parameter from another dropdownlist -> Called This DropDownList B
Everytime DropDownList B Change the selected value, Datasource of Dropdownlist A will receive the parameter and change dropdownlist A.
Whenever DropDownlist change its index, it will cause postback and the selected value will be reset to the first index (default).
After Switching DropDownList A to use a datasource with no parameter. This problems is not occur.
After DropDownList A has changed its value and postback, the recenly changed value still there.
I have tried using update panel but it still is not work either.
This is DropDownList B, Add On Type
<asp:DropDownList ID="ddlAddOnType" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" ClientIDMode="Static" CssClass="controlOrdinary"
DataSourceID="odsAddOnType" DataTextField="AddOnTypeName"
DataValueField="AddOnTypeId"
onselectedindexchanged="ddlAddOnType_SelectedIndexChanged" Width="208px">
<asp:ListItem Value="0">--Please Select--</asp:ListItem>
</asp:DropDownList>
This is datasource for dropDownlist A
<asp:ObjectDataSource ID="odsAddon" runat="server"
SelectMethod="SelectAddOnThatIsNotInCurrentPromotionWithAddOnByAddOnTypeId"
TypeName="BackOfficeLib.SupportAdmin.PromotionBinder">
<SelectParameters>
<asp:ControlParameter ControlID="ddlAddOnType" Name="pIntAddOnTypeId"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
This is dropdownlist A, when this one change the value, the page being post back suddenly and the index is reseted.
<asp:DropDownList ID="ddlAddOn" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" ClientIDMode="Static" CssClass="controlOrdinary"
DataTextField="AddOnName" DataValueField="AddOnId"
onselectedindexchanged="ddlAddOn_SelectedIndexChanged" Width="208px">
<asp:ListItem Value="0">--Please Select--</asp:ListItem>
</asp:DropDownList>
The solution i use is keeping value in hidden field by javascript, using onchange event ( change the first drop down list)
This asp.net ajax control provide some help.
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/CascadingDropDown/CascadingDropDown.aspx

Resources