当我的 HTML 中的“Select”元素在我自己的浏览器中的 C# 应用程序中打开时,我尝试填充该元素。所以我这样做了:
DataTable dt = new DataTable();
HtmlElement States = webBrowser1.Document.GetElementById("Select1");
da.Fill(dt);
States.InnerHtml = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
string head = String.Format(@"<option Value=""{1}"">",
dt.Rows[i]["Code"].ToString());
string body = String.Format("{0}</option>", dt.Rows[i]["Name"].ToString());
string tag = head + body;
States.InnerHtml =tag;
}
但令人惊讶的是,当我期望结果是这样的
< option Value="aNumber">testString<\option>
我看到这个:
testString<\option>
当我努力编写预期的字符串时,我在这里看到了同样的问题。最后,我在字符串的第二个字符中插入了一个空格。因此我写了< option>
而不是<option>
在这里。我对要传递给 InnerHtml
的字符串做了同样的事情我的元素的属性。但不幸的是它没有起作用。
最佳答案
这不应该吗
string head = String.Format(@"<option Value=""{1}"">",
dt.Rows[i]["Code"].ToString());
是这个吗?
string head = String.Format(@"<option Value=""{0}"">",
dt.Rows[i]["Code"].ToString());
关于c# - 将字符串分配给 HtmlElement 对象的 InnerHtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9273179/