c# - Why 'innerhtml' does not work properly for 'select' tag -
i trying set innerhtml
of html select
tag cannot set feature;therefor,i need use outerhtml
feature.this way,not code hardcode
,but preposterous.i have read 'innerhtml ie 8 doesn't work properly? resetting form',it did not though.
i appreciate if tell me how set innerhtml
feature of html select
tag. c# code:
public void setdefaultvalue(string controlid, string controlvalue) { system.windows.forms.htmldocument doc = webbrowser1.document; htmlelement htmlcontrol = doc.getelementbyid(controlid); string listresult; string listinnerhtml = ""; listinnerhtml += "<option value = " + lststring + ">" + lststring + "</option>"; listresult = "<select id = " + '"' + htmlcontrol.id + '"' + " type = " + '"' + htmlcontrol.getattribute("type") + '"' + " title = " + '"' + htmlcontrol.getattribute("title") + '"' + " name = " + '"' + htmlcontrol.name + '"' + " value = " + '"' + htmlcontrol.getattribute("value") + '"' + " size = \"" + htmlcontrol.getattribute("size") + '"' + htmlcontrol.getattribute("multiple").tostring() + "\">" + listinnerhtml + "</select>"; htmlcontrol.outerhtml = listresult; }
or
string _lsthtml = _htmlel.outerhtml; string[] _parts = controlvalue.split(new char[] { ',' }); string _lstinner = ""; foreach (string _lst in _parts) _lstinner += "<option value=" + _lst + ">" + _lst + "</option>"; _lsthtml = _lsthtml.insert(_lsthtml.indexof(">") + 1, _lstinner); _htmlel.outerhtml = _lsthtml;
this code works need efficient , clean. returncontroltype
function returns type
of html tag.
this official internet explorer bug:
bug: internet explorer fails set innerhtml property of select object.
one workaround
you may try adding 1 of following meta
tags in document's head:
<meta http-equiv="x-ua-compatible" content="ie=edge" />
or
<meta http-equiv="x-ua-compatible" content="ie=10" />
you should format option
tag's value
attribute (enclose lststring
in '
):
listinnerhtml += "<option value='" + lststring + "'>" + lststring + "</option>";
a more reliable solution
as fixes above might workaround code, suggest use more reliable approach. consider adding reference microsoft.mshtml project , modifying method this:
// add top of file containing class using mshtml; public void setdefaultvalue(string controlid, string controlvalue) { system.windows.forms.htmldocument doc = webbrowser1.document; ihtmldocument2 document = doc.domdocument ihtmldocument2; var sel = doc.getelementbyid(controlid); htmlselectelement domselect = (htmlselectelement)sel.domelement; domselect.options.length = 0; htmloptionelement option; // here can dynamically add options select element (int = 0; < 10; i++) { option = (htmloptionelement)document.createelement("option"); option.text = string.format("text{0}", i); option.value = string.format("value{0}", i); domselect.options.add(option, 0); } }
Comments
Post a Comment