Drop down list Selected value - c#

I implemented a drop down in my ASP.net page .I put data sourse and bind drop down in page load event . I get the data in the drop down without any problem . But when i selected a value it always send index 1 value . I tried different values but it always sends the index 1 value to the backend
string[] parity = conData.GetParity();
ddlParity.DataSource = parity.ToList();
ddlParity.DataBind();
modemDetailsObj.Parity = ddlParity.SelectedValue;

You are probably binding the dropdown in postback again. You need to bind it in !Postback
if(!Page.IsPostBack)
{
string[] parity = conData.GetParity();
ddlParity.DataSource = parity.ToList();
ddlParity.DataBind();
}

Related

save default value in combobox when nothing is selected c#

In my winform data entry application , I have a combo box set to "Dropdown List" so the user couldn't add any values other than items present in my combo box.
but while saving if the user doesn't select any item in the combobox I need to save the form with default value("Mg") , but whenever I try to save it shows the field can't be null as it doesn't accept null values:
what I've tried during (save button click event) 1:
string unitcb = unitComboBox.Text;
finishUOMComboBox.Text = uomtxt;
try2:
finishUOMComboBox.SelectedText = "Mg";
try3:
finishUOMComboBox.ValueMember = "Mg";
try4:
finishUOMComboBox.Text = "Mg";
try5:
finishUOMComboBox.selctedindex = 0;
please help how to save "mg" in the combbox when nothing is selected or it is null while saving.
Try this way to get ComboBox selected item:
finishUOMComboBox.selectedItem.ToString();
This code returns your selection's value

Filling two nested drop dwon list when editing an entry in c#

i have two nested drop down list.
one drop down show group,the other show sub group.
i have coded data source of subgroup drop down list on group drop down list selected indexed change event.
What I'm trying to do is set a drop down list to be whatever its value is in the database when editing an entry.
i have used data row.
i have two tables.documentgroup(GroupId,parentId,groupTitle,subGroupTitle) and documents(DocID,GroupID,Title,url)
in my drop down lists i have added list item like this
<asp:ListItem Text = "--select group--" Value = ""></asp:ListItem>
when i click on editing a document,i have this.
protected void grdDocuments_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DoEdit")
{
GC.Collect();
GC.WaitForPendingFinalizers();
int DocID = Convert.ToInt32(e.CommandArgument);
ViewState["DocumentID"] = DocID;
ViewState["EditMode"] = "Edit";
DataTable dtDocuments = DataLayer.Documents.SelectRow(DocID).Tables["Documents"];
if (dtDocuments.Rows.Count != 0)
{
DataRow drDocuments = dtDocuments.Rows[0];
txtDocTitle.Text = drDocuments["DocTitle"].ToString();
txtDocPubYear.Text = drDocuments["DocPubYear"].ToString();
ddlDocGroupTitle.SelectedValue = drDocuments["ParentID"].ToString();
ddlDocSubGroupTitle.SelectedValue = drDocuments["DocGroupID"].ToString();
ViewState["DocCurrentUrl"] = drDocuments["DocUrl"].ToString();
mvDocuments.SetActiveView(vwEdit);
}
}
but i get this error
'ddlDocSubGroupTitle' has a SelectedValue which is invalid because it
does not exist in the list of items. Parameter name: value
and this is the same for ddldocgroupTitle.
i have made an inner join between two tables.
what should i do?
I could not really grasp your question, but it looks like you want to have 2 dropdown, and upon selection in the first, the second gets populated/filtered correct?!?
I recommend that you use javascript/Jquery to do so. Upon page load, issue a ajax get request to fill the "main" dropdown, and upon selection on the first one, you issue a second ajax request to fetch the possible values of the second one...
A alternative is to fetch the items for the first dropdown AND all the values possible for the second, storing the result in some hidden-field data. Then upon selection of the first value, you can only filter + display relevant data.

Get selected item of DropDownListFor and using it to create new list

Have some problems trying to solve this. Have two DropDownListFor where the first is populated with data and the second should be populated using the selected value from the first.
Lets say the first DropDownlist contains these data:
RoutesList;
Value = "CoOs", Text = "Copenhagen - Oslo",
Value = "StOs", Text = "Stockholm - Oslo",
Value = "OsCo", Text = "Oslo - Copenhagen"
In my razor view I'm trying to create a new list based on the selected value.
How do I get the selected value, lets say "StOs" and format it so it only contains the first two letters "St"? Trying to do this with C# and not Jquery.
My code
// Loads RoutesList to departureRouteSelectList
var departureRouteSelectList = Model.RoutesList;
// Finds selected value
var selectedItem = departureRouteSelectList.Select(x => x.Value);
// Create new selectList for returnRoute
List<SelectListItem> returnRouteSelectList = null;
I'm not sure if the "Select" command does what I want and getting the "StOs"?
Understanding what you want make me think this post will resolve your problem:
Can't get my DropDownListFor to select a selected SelectListItem item in a Dropdown menu

combobox item selected To be fixed

I had a databound combobox in my windows form I populate it by a function deptload() IN FORM LOAD
public void DeptcomboLoad()
{
DataTable dt = depttrans.getDeptName();
Cmb_Department.DataSource = dt;
Cmb_Department.DisplayMember = "DepartmentName"; //CHAR
Cmb_Department.ValueMember = "DepartmentPK"; //INT
}
Now when an employee of a department (say accounts DepartmentName="Accounts " , DepartmentPK=23 ) login I want the ComboBox text to be selected as "acounts "
and when I go to get the selected value of the ComboBox I should get 23
I tried
Cmb_Department.selectedtext="Accounts"
Cmb_Department.Text="Accounts"
but its not giving the selected value
Can anyone give a suggestion
Instead of trying to put a value INTO the combobox, try to GET the SelectedItem like this:
string txt= Cmb_Department.SelectedItem.Text
or just:
string txt= Cmb_Department.SelectedText
To change selected value of the combobox you can use
SelectedItem property or SelectedIndex.
Index must be exact number in your data sourse, and Item must be exact object from datasource
You can get it to select the right item by issuing something like this:
Cmb_Department.SelectedValue = 23;
Where 23 comes from some other variable, maybe on another object, maybe from a local variable, whatever works in your case.
Now, to get the selected value you can use this statement:
var val = Cmb_Department.SelectedValue;
To get the selected text (which would be the text associated with the value):
var text = ((DataRow)Cmb_Department.SelectedItem)["DepartmentName"];
The reason I'm prescribing the aforementioned is because the SelectedText property is volatile, and the Text property doesn't always work based on how the DropDownStyle is set.
However, some would probably argue to get the same as the aforementioned you could issue this statement:
var text = Cmb_Department.Text;

Is it somehow possible to transfer date from a RadComboBox to another RadComboBox (Different Page)?

I have 3 different RadComboBoxs in one page and the 3 RadComboboxes in another page is it somehow possible to transfer the selected values from the first three Comboboxes to the other Comboboxes (Maybe via URL Javascript ) ?
if you need my code just ask
for a start Try query string:
in Page1.aspx
in some button click event
Response.Redirect("~/Page2.aspx?id1="+RadComboBox1.SelectedValue+"&id2="+RadComboBox2.SelectedValue)
and
in Page2.aspx
in page load event
string cmb1Value = Request.QueryString["id1"].ToString();
string cmb2Value = Request.QueryString["id2"].ToString();
and bind these values to required comboboxes here as below.
//Use RadComboBox.SelectedIndex
int index1 = RadComboBox1.FindItemIndexByValue(cmb1Value );
RadComboBox1.SelectedIndex = index1;
int index2 = RadComboBox2.FindItemIndexByValue(cmb2Value );
RadComboBox2.SelectedIndex = index2;

Categories

Resources