DropDownList Control asp.net SelectedValue - c#

I am Working With DropdownList Control ... First I bind the dropdown Control in Page_Load() using this Code
combo.DataSource = ds3.Tables[0];//combo is the ID of DropDownList Control
combo.DataTextField = ds3.Tables[0].Columns["ProjectName"].ColumnName.ToString();
combo.DataValueField = ds3.Tables[0].Columns["ID"].ColumnName.ToString();
combo.DataBind();
then I use the selected Value in Button click using this Code
protected void btn_newUser_Click(object sender, EventArgs e)
{
cmd1 = new SqlCommand("SP_createUserFroProjects", con);
cmd1.CommandType = CommandType.StoredProcedure;
int m =Convert.ToInt32( combo.SelectedValue);
cmd1.Parameters.AddWithValue("#projectID",m);
cmd1.Parameters.AddWithValue("#userName", txt_User.Text);
cmd1.Parameters.AddWithValue("#password", txt_pass.Text);
cmd1.Parameters.AddWithValue("#name", txt_User_Name.Text);
ds4 = new DataSet();
adpt4 = new SqlDataAdapter(cmd1);
adpt4.Fill(ds4);
When I check the result int m is always equal to 1 in the inspect of the page the html is
<select name="combo" id="combo">
<option selected="selected" value="1">project1</option>
<option value="2">project2</option>
<option value="3">project3 </option>
<option value="4">project4 </option>
<option value="5">lubdbsljv</option>
<option value="10">project5</option>
<option value="1018">test4</option>
<option value="1019">test5 </option>
<option value="1020">test6</option>
<option value="1021">test7</option>
<option value="1022">testtt</option>
<option value="1023">new</option>
<option value="1024">new2</option>
<option value="1025">new5</option>
<option value="1026">next</option>
<option value="1027">new nnn</option>
<option value="1028">ttttt</option>
<option value="1029">new project 5 </option>
<option value="1030">newprj</option>
<option value="1031">projectnewtest</option>
</select>
but it always sends first Value from selected value in the button click

Try this in your code file page load method.
if (!Page.IsPostBack)
{
combo.DataSource = ds3.Tables[0];//combo is the ID of DropDownList Control
combo.DataTextField = "ProjectName";
combo.DataValueField = "ID";
combo.DataBind();
}

In your Page_Load method, if you don't check if it is a postback or not, your code which fills the combobox will execute every time that you click a button or any other control which postbacks the page.
It means that every time that you click on a button, the combobox fill be filled and the selected index will be 0 and the selected value will be the first value of your list.
You should check like this :
private void Page_Load()
{
// The code here will execute on every postback (button click etc..).
if (!IsPostBack)
{
//The code will execute if the page load
}
}

Related

How to get a dropdown with different background colors. Dropdown options are dynamic

I want to get a dynamic dropdown with different backgroundcolors depending on the value. value: red -> backgroundcolor: red and so on...
Dropdown is filled by table in SQL-database.
Ampel
This snippet will do what you need.
$('#mySelect > option').each(function() {
$(this).css('background-color', $(this).val());
});
$('#mySelect').on('change', function() {
$('#mySelect').css('background-color', $(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="mySelect">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
<option value="orange">orange</option>
</select>

Get selected values from DB in select list

I want dropdown auto select value from DB while loading. For example i have values like this:
<option value="1">Mango</option>
<option value="2">Orange</option>
but I want when dropdown load orange selected because in previous form I already select orange.
<select class="form-control" data-plugin="select2" id="ddl_fruit" name="ddl_fruit" disabled="false" data-select2-id="2" tabindex="-1">
<option value="">Select fruit</option>
#if (ViewBag.fruitname!= null)
{
foreach (var item in ViewBag.fruitname)
{
<option value="#item.ID">#item.fruitname</option>
}
}
</select>
I am using ADO.NET and using not html.dropdown.
How do I solve the problem?
foreach (var item in ViewBag.fruitname)
{
if(#item.fruitname == "Orange")
<option value="#item.ID" selected = "selected">#item.fruitname</option>
else
<option value="#item.ID">#item.fruitname</option>
}
if you do not want to use if condition or html helpers then you use Jquery to select specific item.
Note: if you are using sections please put this code in it.
$(function(){ $('#ddl_fruit').val('2') // for orange })
Pass selected value in ViewBag.SelectedFruit from Controller to View.
In View:
foreach (var item in ViewBag.FruitName)
{
<option value="#item.Id" #(ViewBag.SelectedFruit == item.FruitName ? "selected" :"")>#item.FruitName</option>
}

Select Item of webbrowser dropdown list C#

I want select item from dropdown list in winforms C# app.
I have:
<select name ="petbet">
<option value="1">Speedy</option>
<option value="2">Snuffles</option>
<option value="3">Sneak</option>
<option value="4">Snow</option>
<option value="5">Slowww</option>
I try:
var test = webBrowser2.Document.GetElementsByTagName("select").Count;
webBrowser2.Document.Forms[0].SetAttribute("value", "3");
I want select sneak, but don't work.

change the value of a dropdown list

I am trying to change the value of a drop down list using the following code on the click event handler. Nothing actually changes when the button is pressed. What am I missing? Is this the correct way to do this?
HtmlDocument document = webBrowser1.Document;
HtmlElement salutation = document.GetElementById("status");
salutation.SetAttribute("value", "Mr");
Here is the html
<select id="status">
<option selected="selected" value="">Select</option>
<option value="Mr">Mr.</option>
<option value="Mrs">Mrs.</option>
</select>
You could try this (based on your code)
HtmlDocument document = webBrowser1.Document;
HtmlElement salutation = document.GetElementById("status");
var option = salutation.Children.Cast<HtmlElement>().First(x => x.GetAttribute("value").Equals("Mr"));
option.SetAttribute("selected", "selected");
Are you selecting the <select> tag? If so, don't use that. Select the <option>, just like this:
<select>
<option value="volvo">Volvo</option>
<option value="saab" selected="selected">Saab</option>
<option value="vw">VW</option>
<option value="audi">Audi</option>
</select>
To see it in action: See this link
Using some codes from scartag, how about this:
HtmlElement salutation = document.GetElementById("status");
var option = salutation.Children.Cast<HtmlElement>().
Where(x => x.GetAttribute("selected").Equals("selected")).First();
if (option != null) option.SetAttribute("value","Mr,");

Webbrowser - update second dropdown list after choosing an item from the first dropdown list

My goal is to update second dropdown list which is updated after choosing an item from the first dropdown list. Any help would be appreciated.
I can't detect when start ajax activity in webbrowser control. I've tried instructions in this thread with no success, implemented the following class:
HTML:
<html>
<Head>
<script type="text/javascript" src="http://myweb.com/scripts/jquery_1.5.js">
</head>
<body>
<select id =”cboCity”>
<option value=”1”> C1 </option>
<option value=”2”> C2 </option>
<option value=”3”> C3 </option>
<select id =”cboDistrict”>
<option value=”1”> D1 </option>
<option value=”2”> D2 </option>
<option value=”3”> D3 </option>
</body>
</html>
C# Code:
System.Windows.Forms.Timer timer1;
HtmlElement city = webbrowser1.doc.GetElementById("cboCity ");
city.Focus();
city.SetAttribute("value", "2");
city.InvokeMember("onchange");
timer1 = new System.Windows.Forms.Timer();
timer1.Tick +=new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
private void timer1_Tick(object sender, EventArgs e)
{
HtmlElement district = webbrowser1.doc.GetElementById("cboDistrict");
district.Focus();
district.SetAttribute("value", "3");
district.InvokeMember("onchange");
timer1.Stop();
this.timer1.Enabled = false;
}
You can use jquery to do that.
$(function() {
$("#cboCity").change(function() {
var currentSelection = $(this).val();
//grab the other items
//loop trough them
//inert into $("cboDistrict").html();
});
});
Why not use jQuery and the change even on the dropdown?
http://api.jquery.com/change/

Categories