How to set value to the selectbox?http://forums.asp.net/t/1512103.aspx/1?How+to+set+value+to+the+selectbox+Mon, 18 Jan 2010 02:45:00 -050015121033606030http://forums.asp.net/p/1512103/3606030.aspx/1?How+to+set+value+to+the+selectbox+How to set value to the selectbox? <p>Hello,</p> <p>For example I have some control like this:</p> <p><br> </p> &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;select name=&quot;Country&quot; id=&quot;Country&quot;&gt;&lt;option selected=&quot;selected&quot;&gt;USA&lt;/option&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;option&gt;Afghanistan&lt;/option&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;option&gt;Akrotiri&lt;/option&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;option&gt;Albania&lt;/option&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;option&gt;Algeria&lt;/option&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;option&gt;American Samoa&lt;/option&gt;&lt;/div&gt; <p></p> <p>&lt;select name=&quot;Country&quot; id=&quot;Country&quot;&gt;</p> <p>&nbsp;&nbsp; &lt;option selected=&quot;selected&quot;&gt;USA&lt;/option&gt;</p> <p>&nbsp;&nbsp; &lt;option&gt;Afghanistan&lt;/option&gt;&nbsp;</p> <p>&nbsp;&nbsp; &lt;option&gt;Akrotiri&lt;/option&gt;</p> <p>....</p> <p>How can I set value to the selectbox? Just now I can set only index (ex: page.Elements.Find(&quot;Country&quot;).SetSelectedIndex(3) ), but its not useful. I know only &lt;option&gt; value, but don't know index.&nbsp;</p> <p></p> <p><br> </p> 2010-01-08T14:30:00-05:003610200http://forums.asp.net/p/1512103/3610200.aspx/1?Re+How+to+set+value+to+the+selectbox+Re: How to set value to the selectbox? <p>Hello,</p> <p>If your combo&nbsp;ID is Country. so you can pass text or string in place of &quot;USA&quot; in below syntax and set it's index. Hope it will solve your problem, may no me if you require more info.</p> <p>Mark as answer if it helps.</p> <p>int nCountryIndex = Country.Items.IndexOf(Country.Items.FindByValue(&quot;USA&quot;));</p> <p>if (nCountryIndex&nbsp; &gt; -1)</p> <p>Country.Items[nCountryIndex ].Selected = true;</p> <p>&nbsp;</p> <p>Regards,</p> <p>Sandeep</p> 2010-01-11T11:12:53-05:003611277http://forums.asp.net/p/1512103/3611277.aspx/1?Re+How+to+set+value+to+the+selectbox+Re: How to set value to the selectbox? <p>Thanks a lot for the idea!!! It looks a little differently in LTAF:</p> <p></p> &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HtmlElementFindParams myCountryParams = new HtmlElementFindParams(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myCountryParams.TagName = &quot;option&quot;;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myCountryParams.InnerText = &quot;Ukraine&quot;;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HtmlElement myCountry = Country.ChildElements.Find(myCountryParams);&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int nCountryIndex = Country.ChildElements.IndexOf(myCountry);&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (nCountryIndex &gt; -1)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Country.SetSelectedIndex(nCountryIndex); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; <p></p> <p><br> </p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HtmlElementFindParams&nbsp;countryFindParams&nbsp;= new HtmlElementFindParams(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;countryFindParams.TagName = &quot;option&quot;;</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;countryFindParams.InnerText = &quot;USA&quot;;</p> <p><br> </p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int nCountryIndex = Country.ChildElements.IndexOf(Country.ChildElements.Find(countryFindParams));</p> <p><br> </p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (nCountryIndex &gt; -1)</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Country.SetSelectedIndex(nCountryIndex); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p> &lt;div&gt;<br> &lt;/div&gt;&lt;div&gt;-----&lt;/div&gt;&lt;div&gt;Dmitriy&lt;/div&gt; <p></p> <p></p> 2010-01-11T21:19:18-05:003623319http://forums.asp.net/p/1512103/3623319.aspx/1?Re+How+to+set+value+to+the+selectbox+Re: How to set value to the selectbox? <p>Hello Dmitriy,</p> <p>I would say LTAF is missing an API to make your scenario simpler. I will look into adding it in a future release.</p> <p>One thing that you could do to make this bearable is to add this function as an extension method to HtmlElement. For example:</p> <p>public static void SelectSelectedText(this HtmlElement element, string text)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HtmlElementFindParams findParams = new HtmlElementFindParams();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; findParams.TagName = &quot;option&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; findParams.InnerText = text;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int index = element.ChildElements.IndexOf(element.ChildElements.Find(findParams));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (index &gt; -1)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; element.SetSelectedIndex(index);&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; }</p> <p><br> </p> <p>- Federico<br> </p> 2010-01-18T02:45:00-05:00