Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Guys,

I am facing a chalenge in setting a value for a tagelment using setAttribute function.
I had created webrowser,then i load an html page init.Then i collect the elments of that page using HtmlElementCollection then when i try to set the value for a particular elment using setAttribute its not workin a blank is selected.This is happening for drop down box elemnts only for text boxes its working fine.Here is the code

C#
void SetCombo(string attribute, string attName, string value)
        {
            HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName("select");
            foreach (HtmlElement currentTag in tagsCollection)
            {
                if (currentTag.GetAttribute(attribute).Equals(attName))
                    currentTag.SetAttribute("value", value);

            }
        }


Please help me
Posted

1 solution

value attribute is not present for select control.You can select the option by setting the option index.
In the below code option 4 will be selected.

currentTag.SetAttribute("selectedIndex", "4"); 


You can also parse through the child option controls and set the attribute selected as "selected"

Please find the code snippet.
 HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName("select");
            foreach (HtmlElement currentTag in tagsCollection)
            {
                if (currentTag.GetAttribute(attribute).Equals(attName))
                {
                    HtmlElementCollection optionCollection = currentTag.Document.GetElementsByTagName("option");
                    foreach (HtmlElement optionTag in optionCollection)
                    {
                        if (optionTag.GetAttribute("value").Equals("Thevalue you want to check"))
                            optionTag.SetAttribute("selected", "selected");

                    }
                }

            }
 
Share this answer
 
v2
Comments
muhammed_k 9-Sep-12 3:57am    
Thanks dude,

Is there any way to get the Index of a particular value in that combobox?
else i have to do hardcoding for each values of the drop down list (such as nationality selection).

Please tell me to set the attribute of the file browser feild also.
Ashraff Ali Wahab 9-Sep-12 9:32am    
1.parse through the child options and use optionTag.getAttribute("value", value) and check the value is same as what you want to set,if so set the
optionTag attribute as selected.

2.File browser value cannot be set programmatically because of security issue.
muhammed_k 9-Sep-12 12:56pm    
can you please tell me how is child controls used,sorry i don't have knowledge in it.
Is there any way for the file browse control by over comming security.
Ashraff Ali Wahab 9-Sep-12 13:23pm    
code snippet provided in the solution.File Browser control is desinged that way and you cant do it.
muhammed_k 10-Sep-12 8:27am    
Can you please provide me a code snippet to access the child tags.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900