Select an item from the drop-down list - c#

Good morning.
I can not select an item from the drop-down list.
Did the following:
1) I entered the text
WebBrowser1.Document.GetElementById ("Advert_category_id").InnerText = "Wedding / Event Services";
2) Set the value using the following commands, in different sequences, as soon as not twisted.
WebBrowser1.Document.GetElementById("Advert_category_id").SetAttribute("selected", "selected");
WebBrowser1.Document.GetElementById("Advert_category_id").InvokeMember("onchange");
WebBrowser1.Document.GetElementById("Advert_category_id").InvokeMember("submit");
WebBrowser1.Document.GetElementById("Advert_category_id").InvokeMember ("value", "84");
The problem is that this code selects the option I need, but it does not appear in the field. Those. Only when you click on the field, after executing the code, you can see that the desired option is selected. But this approach is also not true, because
The following drop-down list named SUB CATEGORY does not load the required data (the data remains with the category selected by default - Professional)
3. I tried to experiment with different id-shniki. Nothing helps :(
The site itself https://list.asiandirectoryapp.com/advert/create
(You must register to view)
I made the second ack,
That would not waste time for registration
fovogaze#p33.org
qwerty123
The code of interest
<div style="display:none"><div class="form-group"><label class="control-label" for="Advert_city_id">City</label><select class="form-control" name="Advert[city_id]" id="Advert_city_id">
</select><div class="help-block error" id="Advert_city_id_em_" style="display:none"></div></div></div><div class="form-group"><label class="control-label required" for="Advert_category_id">Category <span class="required">*</span></label><select class="form-control" name="Advert[category_id]" id="Advert_category_id">
<option value=""></option>
<option value="35">Beauty, Health & Fitness</option>
<option value="19">Education</option>
<option value="21">Entertainment</option>
<option value="27">Food, Drink & Sweet Centres</option>
<option value="39">Home Services</option>
<option value="116">Jobs/Careers</option>
<option value="43">Motoring</option>
<option value="50">Other Services</option>
<option value="1">Professional</option>
<option value="51">Property Improvements</option>
<option value="84">Wedding/Event Services</option>
<option value="117">Whats On Guide</option>
</select><div class="help-block error" id="Advert_category_id_em_" style="display:none"></div></div><div class="form-group"><label class="control-label required" for="Advert_categoryList">Sub category <span class="required">*</span></label><select multiple="multiple" class="form-control" name="Advert[categoryList][]" id="Advert_categoryList">
<option value="2">Accountants</option>
<option value="4">Claims</option>
<option value="123">Clothing Manufacturers</option>
<option value="6">Estate & Letting Agents</option>
<option value="7">Finance/Mortgages</option>
<option value="8">Graphic & Web Design</option>
<option value="9">Immigration</option>
<option value="10">Information Technology</option>
<option value="11">Insurance</option>
<option value="13">Mobile Apps - Mobile Phones</option>
<option value="14">Online Marketing/ SEO</option>
<option value="18">Print</option>
<option value="15">Retail and Wholesale</option>
<option value="16">Solicitors - Lawyers</option>
<option value="17">Travel Agents</option>
</select><div class="help-block error" id="Advert_categoryList_em_" style="display:none"></div></div><div class="form-group"><label class="control-label required" for="Advert_name">Company Name <span class="required">*</span></label><input class="form-control" placeholder="Company Name" name="Advert[name]" id="Advert_name" type="text" maxlength="100" /><div class="help-block error" id="Advert_name_em_" style="display:none"></div></div>
P.S. I can fill out all the fields, press the buttons the same way, but with the drop-down list the trouble. Thank you for any help.

I had a similar problem but I chose to solve it by simply invoking a client-side javascript (with a parameter) that handles the manipulation of the dom objects.
I found that this way I can separate code too, keeping dom handling in the JS and application-code in the winforms. Itll also allow you to leverage any functionality provided by e.g. jQuery plugins wrapping the select.
It could look like this (not tested):
Object[] objArray = new Object[2];
objArray[0] = selectorID;
objArray[1] = valueToSet;
webBrowser1.Document.InvokeScript("SetSelector", objArray);
and in the client side:
function SetSelector(selectorID, valueToSet){
$('#'+selectorID).val(valueToSet); //or any function you'd like
}

Related

Create a dynamic form using dynamic SelectListItem dropdown lists in ASP.NET core

I am using ASP.NET core and I'm trying to create a dynamic filter form based on the input values I get from the form. If the user selects a certain value, the View is supposed to render a number of dropdown select options based on the inputs given.
I have a sample of dropdown lists already initialized in my controller as shown.
public IActionResult DataFilter(){
//these are SelectListItem lists with Text and Value
ViewBag.studentDropdown = dropdownContent.StudentsDropdown();
ViewBag.subjectDropdown = dropdownContent.SubjectDropdown();
ViewBag.gradeDropdown = dropdownContent.GradeDropdown();
return View();
}
The input field in the view would look like this
<form ..>
....
<select name="filter-field" class="form-control">
<option value=1>Student report</option>
<option value=2>Subject report</option>
<option value=3>Grade report</option>
</select>
<button class="btn btn-info" type="submit">Get filters</button>
</form
How can I make it that if a user selects option 1, Student reports, the form looks like
<div class="form-group col-md-4"><label class="control-label">Student list</label>
<select name="students" class="form-control" asp-items="#(new SelectList(ViewBag.studentDropdown, "Value", "Text"))">
<option value="">Select an option...</option
</select>
</div>
If a user selects option 2 subject report, the resultant form looks like this
<div class="form-group col-md-4"><label class="control-label">Student list Module</label>
<select name="students" class="form-control" asp-items="#(new SelectList(ViewBag.studentDropdown, "Value", "Text"))">
<option value="">Select an option...</option
</select>
</div>
<div class="form-group col-md-4"><label class="control-label">Subject list</label>
<select name="students" class="form-control" asp-items="#(new SelectList(ViewBag.subjectDropdown, "Value", "Text"))">
<option value="">Select an option...</option
</select>
</div>
There are many input fields with varied filter parameters so the number of select list items will vary.
I tried placing all the lists in one list and saving their indexes in the database so that when a value is selected from the filter field form, it retrieves the indexes as an array and loops through them, but it resulted in an error.

Problems with <select> filter

i coded an select filter for my website:
Filter
<div class="row">
<select class="event-type-select" id="selection" >
<optgroup label="categorygroup1">
<option value="category1">category1</option>
</optgroup>
<optgroup label="categorygroup2">
<option value="category2">category2n</option>
<option value="category3">category3</option>
</optgroup>
<optgroup label="categorygroup3">
<option value="category4">category4</option>
</optgroup>
</select>
</div>
<div class="row">
<button type="submit" class="btn btn-default"> filter </button>
</div>
</form>
before selecting anything in the "selection-window" on the website you read "category1". i guess thats default(?).
but if i selected a category (and that works: you can read in the "selection-window" e.g. category3) and then click on the "filter"-button the website refreshes but in the "selection-window" you again read "category1" and not the selected category e.g "category3". i guess the selection doesn't work right.
how can i solve this that when clicking the button you still read your selected category?
also i want to display the selected option on a different part of the website.
my current code for this:
<script type='text/javascript'>
var m = document.getElementById("selection").value;}
document.write(m);
</script>
that as well doesn't work.
i hope you can understand what i mean, my english is pretty bad. sorry for that.
First, the question essentially contains 2 questions packed into one, don't do that.
Second, you are simply missing the preselection of the <select> field.
the selection doesn't work
When submitting a form the values are sent to the server. The server processes the values and renders the page again. Therefore you have to adjust the rendering according to the selected value. Lets say you simply pass the selected value to the view as string the view code could look like this:
#model string
...
<select class="event-type-select" id="selection" >
<optgroup label="categorygroup1">
<option value="category1" #if (Model == "category1") {<text>selected</text>}>category1</option>
</optgroup>
<optgroup label="categorygroup2">
<option value="category2" #if (Model == "category2") {<text>selected</text>}>category2n</option>
...
</select>
also i want to display the selected option on a different part of the website.
Then store the selection somewhere (e.g. a DB), retrieve the selected value before the different part is rendered and simply output it in the different part.
The script has an error, you should use a function:
function test()
{
var m = document.getElementById("selection").value;
alert(m);
}
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div class="row">
<select id="selection" class="event-type-select">
<optgroup label="categorygroup1">
<option value="category1">category1</option>
</optgroup>
<optgroup label="categorygroup2">
<option value="category2" selected="selected">category2n</option> <!-- with selected="selected" you set the default -->
<option value="category3">category3</option>
</optgroup>
<optgroup label="categorygroup3">
<option value="category4">category4</option>
</optgroup>
</select>
</div>
<div class="row">
<button type="submit" class="btn btn-default" onclick="test()"> filter </button>
</div>
</body>
</html>

c# selecting a listbox option with webbrowser

Im trying to write an application where web form will be filled with one click. The problem is that I can't get working picking one option from listbox, here is HTML code I'm facing:
<select name="data[PvpnetAccount][date_of_birth_day]" id="PvpnetAccountDateOfBirthDay" class="ignore_keyup c-signup__input c-signup__input--select c-form-group__input c-form-group__input--select js-selectize">
<option value="" disabled selected >Day</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
I have tried with different options, but nothing gave me results so far, so I'm up for any suggestions. My first try was:
webBrowser1.Document.GetElementById("PvpnetAccountDateOfBirthDay").SetAttribute("value", "03");
However Im aware that it won't work.
EDIT:
A guy advised me to do this like this:
var daySelect = WebBrowser1.Documents.GetElementById("PvpnetAccountDateOfBirthDay");
var day10Option = daySelect.Children.Single( e => e.Value == "10" );
day10Option.SetAttribute("selected", "selected");
However it seems like .Single gives me error.
Just as a precaution for test purposes to make sure that page loaded correctly and you got correct element selected look at outerHTML property of the element. Also, are you executing this code on background worker, if yes you will need to use Action let me know if you need more instructions on that. Also try using IHTMLSelectElement instead of HtmlElement. To use it add reference to
Microsoft.mshtml
and add using mshtml; to your file. After you can do the following:
HtmlElement tmp = webBrowser1.Document.GetElementById("PvpnetAccountDateOfBirthDay");
IHTMLSelectElement tComboBox = (IHTMLSelectElement)tmp.DomElement;
tComboBox.selectedIndex = 2;
Hope that helps.

Select an item from a drop down in GeckoFx

Html code/example of dropdown:
<select class="ng-valid ng-dirty ng-touched" style="max-width: 235px" ng-model="blogerj" ng-options="blogerj.long_name for blogerj in blogerj track by blogerj.id">
<option class="" value="">[Choose...]</option>
<option label="label 1" value="1">Text Content 1</option>
<option label="label 2" value="2">Text Content 2</option>
<option label="label 3" value="3">Text Content 3</option>
<option label="label 4" value="4">Text Content 4</option>
</select>
If I just set attribute making it selected then it dont work because button for submit appears only when element is selected.
I also tried just to click element to open dropdown and then again click desired option but again it gave me no results.
foreach (GeckoHtmlElement geckoHtmlElement in gwBrowser.Document.GetElementsByTagName("select"))
{
if (geckoHtmlElement.GetAttribute("class") == "ng-valid ng-dirty ng-touched")
{
geckoHtmlElement.Click();
}
}
Actually it finds element but clicking dont do anything.
So maybe there is any fancy js method to select that dropdown?
From the portion class="ng-valid ng-dirty ng-touched" it looks like there is AngularJS at work here - because angular usually uses the prefix ng-. Angular can intercept user actions and take control of how your web page controls behave.

Asp.Net DropDownList problem with apostrophies

I'm adding a record to a DropDownList with the following code
this.ddlLocation.Items.Add(new ListItem(hs.ToString(), ll.id));
This works correctly for normal strings however if there is an apostrophe in the string it comes out in html as
<option value="11">Queen&amp;apos;s University</option>
where it should have been Queen's University. I have checked the value immediately before passing the string and it's how it should be. It looks like it is being encoded twice so the original apostrope is taken out followed by the ampersand exit character. I'm not sure where the encodes are happening and how to miss one of them out.
Also I'm using .Net 4.0
Thanks
Added the full html for the drop down.
<div class="fieldCol">
<label for="peopleSearch_lawSchool">LawSchool</label>
<select name="ctl00$nrContentPlaceHolder$ddlLawSchool" id="nrContentPlaceHolder_ddlLawSchool" class="peopleSearch_lawSchool" name="peopleSearch_lawSchool">
<option value="0">All</option>
<option value="1">Boston University</option>
<option value="2">Columbia University</option>
<option value="3">Cornell Law School</option>
<option value="4">Dalhousie University</option>
<option value="5">HEC Montréal</option>
<option value="6">London School of Economics and Political Science</option>
<option value="7">Loyola University</option>
<option value="8">Magdalene College - Cambridge</option>
<option value="9">McGill University</option>
<option value="10">Osgoode Hall Law School</option>
<option value="11">Queen&amp;apos;s University</option>
<option value="12">Symbiosis Law College</option>
<option value="13">Université de Moncton</option>
<option value="14">Université de Montréal</option>
<option value="15">Université de Sherbrooke</option>
<option value="16">Université du Québec à Montréal</option>
<option value="17">Université Laval</option>
<option value="18">Université Paris X</option>
<option value="19">University of Alberta</option>
<option value="20">University of Birmingham</option>
<option value="21">University of British Columbia</option>
<option value="22">University of Bucharest</option>
<option value="23">University of Calgary</option>
<option value="24">University of California</option>
<option value="25">University of Cambridge</option>
<option value="26">University of Detroit Mercy School of Law</option>
<option value="27">University of Glasgow</option>
<option value="28">University of London</option>
<option value="29">University of London, England</option>
<option value="30">University of Manitoba</option>
<option value="31">University of New Brunswick</option>
<option value="32">University of Ottawa</option>
<option value="33">University of Oxford</option>
<option value="34">University of Pennsylvania</option>
<option value="35">University of Pune</option>
<option value="36">University of Toronto</option>
<option value="37">University of Victoria</option>
<option value="38">University of Western Ontario</option>
<option value="39">University of Windsor</option>
</select>
</div>
It is using a asp:dropdownlist
I'm not sure if you can suppress encoding in the DropDownList, so I'd recommend looking at where did hs get encoded to see if you can fix that.

Categories

Resources