1

在 aspx 页面中,我使用以下内容:

<asp:GridView ID="grdCreateCustRepoUsers">
  <asp:TemplateField>
    <ItemTemplate>
      <div class="cst_checkbox_container1" id="cs1" runat="server">
        <div class="cst_checkbox unselected">
          <label class="checkbox_value">
          <asp:CheckBox ID="chkRead" runat="server" AutoPostBack="true" OnCheckedChanged="chkRead_CheckChanged" /></label>
        </div>
      </div>
    </ItemTemplate>
   </asp:TemplateField>
 </asp:GridView>

在我使用的代码后面:

 cs1.Attributes.Add("class", "some-class");

但它给出的错误cs1在当前上下文中不存在。

我该如何解决这个问题?

4

3 回答 3

3

cs1将不会直接用于页面,因为它将DataSource为您的grdCreateCustRepoUsers GridView.

您需要做的是将其设置RowDataBoundGridView.

在 C# 中使用来自 MSDN 的示例

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
     HtmlGenericControl div = (HtmlGenericControl)e.FindControl("cst_checkbox_container1");
     div.Attributes["class"] = "some_class";
  }
}

这是在VB.NET中(我最初写的,因为我没有注意到你aspx.cs在标题中)

Sub grdCreateCustRepoUsers_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles grdCreateCustRepoUsers.RowDataBound
  If e.Row.RowType = DataControlRowType.DataRow Then
    Dim div as HtmlGenericControl = e.FindControl("cst_checkbox_container1")
    div.Attributes("class") = "some-class"
  End If
End Sub

(注意,我对GridView控件的经验非常有限,但这是基于我对类似<asp:Repeater>控件的经验。因此,上面的代码未经测试,但希望为您指明正确的方向。)

于 2013-02-22T12:09:24.110 回答
0
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HtmlContainerControl div = e.Row.FindControl("cs1") as HtmlContainerControl;
        div.Attributes["class"] = "some_class";
    }
}
于 2013-02-22T12:22:59.643 回答
-1

句法:

controlName.CssClass="CSS Class Name";

例子:

txtBank.CssClass = "csError";
于 2013-07-06T08:05:47.013 回答