How can I set value to the selectbox? Just now I can set only index (ex: page.Elements.Find("Country").SetSelectedIndex(3) ), but its not useful. I know only <option> value, but don't know index.
If your combo ID is Country. so you can pass text or string in place of "USA" in below syntax and set it's index. Hope it will solve your problem, may no me if you require more info.
Mark as answer if it helps.
int nCountryIndex = Country.Items.IndexOf(Country.Items.FindByValue("USA"));
if (nCountryIndex > -1)
Country.Items[nCountryIndex ].Selected = true;
Regards,
Sandeep
Please let me know if you need more help.
Please give us feedback no matter whether you get your answer.
Please "Mark as Answer" if it's useful for you.
Dmitriy Kozh...
Member
14 Points
5 Posts
How to set value to the selectbox?
Jan 08, 2010 02:30 PM|LINK
Hello,
For example I have some control like this:
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><select name="Country" id="Country"><option selected="selected">USA</option></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><option>Afghanistan</option></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><option>Akrotiri</option></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><option>Albania</option></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><option>Algeria</option></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><option>American Samoa</option></div><select name="Country" id="Country">
<option selected="selected">USA</option>
<option>Afghanistan</option>
<option>Akrotiri</option>
....
How can I set value to the selectbox? Just now I can set only index (ex: page.Elements.Find("Country").SetSelectedIndex(3) ), but its not useful. I know only <option> value, but don't know index.
Das.Sandeep
Star
10652 Points
1897 Posts
Re: How to set value to the selectbox?
Jan 11, 2010 11:12 AM|LINK
Hello,
If your combo ID is Country. so you can pass text or string in place of "USA" in below syntax and set it's index. Hope it will solve your problem, may no me if you require more info.
Mark as answer if it helps.
int nCountryIndex = Country.Items.IndexOf(Country.Items.FindByValue("USA"));
if (nCountryIndex > -1)
Country.Items[nCountryIndex ].Selected = true;
Regards,
Sandeep
Please give us feedback no matter whether you get your answer.
Please "Mark as Answer" if it's useful for you.
Regards,
Sandeep
Dmitriy Kozh...
Member
14 Points
5 Posts
Re: How to set value to the selectbox?
Jan 11, 2010 09:19 PM|LINK
Thanks a lot for the idea!!! It looks a little differently in LTAF:
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> HtmlElementFindParams myCountryParams = new HtmlElementFindParams(); </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> myCountryParams.TagName = "option";</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> myCountryParams.InnerText = "Ukraine";</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> HtmlElement myCountry = Country.ChildElements.Find(myCountryParams);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> int nCountryIndex = Country.ChildElements.IndexOf(myCountry);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> if (nCountryIndex > -1)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Country.SetSelectedIndex(nCountryIndex); </div> <div></div>HtmlElementFindParams countryFindParams = new HtmlElementFindParams();
countryFindParams.TagName = "option";
countryFindParams.InnerText = "USA";
int nCountryIndex = Country.ChildElements.IndexOf(Country.ChildElements.Find(countryFindParams));
if (nCountryIndex > -1)
Country.SetSelectedIndex(nCountryIndex);
<div></div><div>-----</div><div>Dmitriy</div>
farmas
Participant
1164 Points
259 Posts
Microsoft
Re: How to set value to the selectbox?
Jan 18, 2010 02:45 AM|LINK
Hello Dmitriy,
I would say LTAF is missing an API to make your scenario simpler. I will look into adding it in a future release.
One thing that you could do to make this bearable is to add this function as an extension method to HtmlElement. For example:
public static void SelectSelectedText(this HtmlElement element, string text)
{
HtmlElementFindParams findParams = new HtmlElementFindParams();
findParams.TagName = "option";
findParams.InnerText = text;
int index = element.ChildElements.IndexOf(element.ChildElements.Find(findParams));
if (index > -1)
{
element.SetSelectedIndex(index);
}
}
- Federico