I have a div (id=main), it contains 15 html select controls inside and their IDs are ddl1, ddl2, ddl3, … and so on.
Now I want to dynamically select or assign their indexes from an XML file, so I wrote this code.
private void readxml(string spath) {
XmlDocument doc = new XmlDocument();
doc.Load(spath);
//doc.LoadXml(spath);
XmlNodeList xmlnodes = doc.SelectNodes("/Hedge/*");
for (int i=1; i <= 15; i++) {
(main.FindControl("ddl" + i) as DropDownList).SelectedIndex = Int32.Parse(xmlnodes[i].InnerText);
}
}
But an error is occuring here…
Object reference not set to an instance of an object.
It’s maybe because it’s not able to find the controls (HTML select controls)…
Can anyone tell me the reason or solve my problem?
There are two thing that may have went wrong.
One is obvious
Since you are not using asp.net dropdown list so you can not cast it into this.
So it should be
Another could be the attribute
runat="server"which needs to be there if you are suinghtml controlsand want to access it on C# page.