change the value of a dropdown list - c#

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,");

Related

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>
}

DropDownList Control asp.net SelectedValue

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
}
}

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.

How to post multiple Select values using HttpWebRequest in Asp.Net

my goal is to use Asp.Net's HttpWebRequest to submit multiple values to a webpage, as if they were sent from a <select> element.
The code i use for creating the data string is as follows:
NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
outgoingQueryString.Add("username", "myusername");
outgoingQueryString.Add("password", "mypassword");
string postData = outgoingQueryString.ToString();
Now lets say I have the following select for example:
<select name="banana" multiple="">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
I need to submit options 1 and 2, how do i add this to the query string? as an array, or can i add several values with the same name?
Thanks in advance.
You can pass comma separed values, because when you do actually post a multi select control, you get comma separated values in Request, so to emulate, you can pass comma separated values.
Following HTML used. Note select is a server control here.
<form id="form1" runat="server">
<div>
<select name="banana" multiple="" id="sl" runat="server">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<asp:Button ID="btn" runat="server" Text="sub" OnClick="btn_Click" />
</div>
</form>
You can simply Use ArrayList
ArrayList MyArray;
if (Request.QueryString["MyArray"] != null)
{
MyArray = (ArrayList)Request.QueryString["MyArray"];
}
else
{
MyArray = new ArrayList();
}
Add values to ArrayList
if (MyArray.IndexOf(checkAllIndex) == -1)
{
MyArray.Add(checkAllIndex);
}
Remove from ArrayList
if (MyArray.IndexOf(checkAllIndex) != -1)
{
MyArray.Remove(checkAllIndex);
}

Html Agility Pack - <option> inner text

I have problem with this html:
<select id="attribute1021" class="required-entry super-attribute-select" name="super_attribute[1021]">
<option value="">Choose an Option...</option>
<option value="281">001 Melaike</option>
<option value="280">002 Taronja</option>
<option value="289">003 Lill</option>
<option value="288">004 Chèn</option>
<option value="287">005 Addition</option>
<option value="286">006 Iskia</option>
<option value="285">007 Milele</option>
<option value="284">008 Cali</option>
<option value="283">009 Odessa</option>
<option value="282">010 Manaus</option>
<option value="303">011 Nartiss</option>
<option value="302">012 Curitiba</option>
<option value="301">013 Bogota</option>
<option value="300">014 Solèy</option>
<option value="299">015 Campinas</option>
<option value="298">016 Formosa</option>
<option value="297">017 Valencia</option>
<option value="296">018 Candu</option>
<option value="295">019 Medellín</option>
<option value="294">020 Incubo</option>
<option value="293">021 Belisama</option>
<option value="292">022 Amo</option>
<option value="291">023 Chimaira</option>
<option value="290">024 Matanza</option>
<option value="319">025 Baltimore</option>
</select>
With this code in C#
foreach (HtmlNode node in dok.DocumentNode.SelectNodes("//select[#class='required-entry super-attribute-select']/option"))
{
sb.Append("V")
.Append(y)
.Append(">")
.Append(node.InnerText)
.Append("/V")
.Append(y)
.Append(">")
.AppendLine();
}
But in inner text is only "Choose an Option..." .
Any idea how to fix it ?
Html Agility Pack by default leaves option-Tags empty. To have it work you need remove the option-Tag from the list of elements that are left empty.
Just put the following somwhere before you load the Html.
HtmlNode.ElementsFlags.Remove("option");
var dok = new HtmlDocument();
dok.Load("option.htm");
var sb = new StringBuilder();
var y = "";
foreach (HtmlNode node in dok.DocumentNode.SelectNodes("//select[#class='required-entry super-attribute-select']/option"))
{
sb.Append("V")
.Append(y)
.Append(">")
.Append(node.InnerText)
.Append("/V")
.Append(y)
.Append(">")
.AppendLine();
}
You need to get at the #text node. Try using this instead:
sb.Append("V")
.Append(y)
.Append(">")
.Append(node.NextSibling.InnerText)
.Append("/V")
.Append(y)
.Append(">")
.AppendLine();
Or you may change your XPath expression to "//select[#class='required-entry super-attribute-select']/option/following-sibling::text()"

Categories