找到具有相同ID“cb1”的多个控件。 FindControl要求控件具有唯一ID

时间:2013-09-24 11:43:26

标签: asp.net

我将数据表值绑定到gridview。我已经在aspx页面中添加了三个templatefield列,并且我通过使用itemplate interface.everythings创建列来添加两个动态复选框列,直到databind.but我想从datatable值绑定checkbox值。如果datatable值为true,则选中复选框值。为此,我尝试了以下代码。

DataTable bindclassattendance = inter.getstudentattendresult(comp);

Createcolumn();

if (bindclassattendance.Rows.Count>0 )
{
Classattendancegrid.DataSource = bindclassattendance;
    Classattendancegrid.DataBind();

    for (int i = 0; i < bindclassattendance.Rows.Count; i++)
    {
        bool check1 = Convert.ToBoolean(bindclassattendance.Rows[i]["AM"].ToString());
        if (check1)
        {
            CheckBox chk = Classattendancegrid.Rows[i].FindControl("AM") as CheckBox;
            chk.Checked = true;
        }
        else
        {
            CheckBox chk = Classattendancegrid.Rows[i].FindControl("AM") as CheckBox;
            chk.Checked = false;
        }
        bool check2 = Convert.ToBoolean(bindclassattendance.Rows[i]["PM"].ToString());
        if (check2)
        {
            CheckBox chk = Classattendancegrid.Rows[i].FindControl("PM") as CheckBox;
            chk.Checked = true;
        }
        else
        {
            CheckBox chk = Classattendancegrid.Rows[i].FindControl("PM") as CheckBox;
            chk.Checked = false;
        }

    }

动态列Createcolumn()是:

private void Createcolumn()
{
    TemplateField amtemp = new TemplateField();
    amtemp.ShowHeader = true;
    amtemp.HeaderText = "AM";
    amtemp.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "AM", "AM", "CheckBox");
    Classattendancegrid.Columns.Add(amtemp);

    TemplateField pmtemp = new TemplateField();
    pmtemp.ShowHeader = true;
    pmtemp.HeaderText = "PM";
    pmtemp.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "PM", "PM", "CheckBox");
    Classattendancegrid.Columns.Add(pmtemp);
}

发生上述错误。怎么解决它,我从来没有使用任何名称'cb1'。该错误发生在行

之后
CheckBox chk = Classattendancegrid.Rows[i].FindControl("AM") as CheckBox;

我的aspx页面是

<asp:GridView ID="Classattendancegrid" runat="server" 
              AutoGenerateColumns="False" onrowdatabound="Classattendancegrid_RowDataBound" 
              style="margin-left: 184px; margin-top: 27px">
  <Columns>
      <asp:TemplateField HeaderText="AdmissionNumber">
          <ItemTemplate>
              <asp:Label ID="lbladmissionnumber" runat="server" 
                  Text='<%#Eval("AdmissionNumber") %>'></asp:Label>
          </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="RollNumber">
          <ItemTemplate>
              <asp:Label ID="lblrollno" runat="server" Text='<%#Eval("Rollnumber") %>'></asp:Label>
          </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="StudentName">
          <ItemTemplate>
              <asp:Label ID="lblStudentName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
          </ItemTemplate>
      </asp:TemplateField>
</column>
</asp:GridView)

0 个答案:

没有答案