Add custom attribute to an HTML input element with C# [duplicate]

南笙酒味 提交于 2021-02-08 10:34:01

问题


I'm using the following method to add a custom attribute to a check box:

CheckBox chkBx = new CheckBox();
chkBx.ID = "chk" + n;
chkBx.Attributes["itemname"] = strtemPath;

But as a result I'm getting the following HTML:

<span itemname="Some folder"><input id="MainContent_chk0" type="checkbox" name="MasterPg$MainContent$chk0" /></span>

where I'm expecting this:

<input id="MainContent_chk0" type="checkbox" name="MasterPg$MainContent$chk0" itemname="Some folder" />

Any idea how to correct the C# code to get my expected result?


回答1:


Try something like

chkBx.InputAttributes.Add("itemname", strtemPath);



回答2:


Try:

chkBx.Attributes.Add("itemname", strtemPath);
  • AttributeCollection.Add Method


来源:https://stackoverflow.com/questions/18075037/add-custom-attribute-to-an-html-input-element-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!