我正在尝试在我的代码隐藏中检索<input>
字段的css类,但是以下两个调用都返回NULL,即使页面的元素显然在标记中有一个类。
if (d.GetType() == typeof(TextBox))
{
TextBox txtComments = (TextBox)d;
string className = txtComments.CssClass; //NULL
string className2 = txtComments.Attributes["class"]; //NULL
string id = txtComments.ID; //Does work correctly
string commentText = txtComments.Text; //Does work correctly
}
标记很简单,当我在Chrome中检查时,它会清楚地显示一个类。
<input name="rptReviewTask$ctl00$rptReviewTaskDetails$ctl01$2" type="text" value="com 2" id="rptReviewTask_rptReviewTaskDetails_0_2_1" placeholder="Comments" class="4" style="width:100%">
元素id在这里以动态方式添加到页面中:
TextBox txtComments = new TextBox();
txtComments.ID = ReviewTaskId.ToString();
txtComments.Attributes.Add("placeholder", "Comments");
txtComments.Attributes.Add("style", "width:100%");
txtComments.Attributes.Add("class", "4");
txtComments.Text = comments;
divTask.Controls.Add(txtComments);