0
<asp:GridView ID="science_tab" runat="server" AutoGenerateColumns="false"   ShowHeader="false" class="subjects_tab">
<Columns>
<asp:TemplateField>
<ItemTemplate> 
    <asp:CheckBox ID="sciencefees_check" runat="server" Text='<%# bind("sub_name") %>' ></asp:CheckBox><%-- i tried binding the text to custom name like Text="anyname" --%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<label class="rsperhr" for="sciencefees_text">Rupees</label>
    <asp:TextBox ID="sciencefees_text" runat="server" Width="40px"  ></asp:TextBox>
    <label class="rsperhr" for="sciencefees_text">Per Hour</label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

and a label and button..

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

this is the on button onclick event:

protected void Button1_Click(object sender, EventArgs e)
{
     for (int i = 0; i < science_tab.Rows.Count; i++)
     {
          GridViewRow row = science_tab.Rows[i];
          bool isChecked = ((CheckBox)row.FindControl("sciencefees_check")).Checked;
          if (isChecked)
          {
               Label1.Text += i + " checked  ";
          }
          else
          Label1.Text += i + " unchecked  ";
      }
}

does not matter how many checkboxes i check if statement always goes for false... label1.Text gives the result 0 unchecked 1 unchecked 2 unchecked ...

4

1 回答 1

0

尝试这个...

protected void Button1_Click(object sender, EventArgs e)
{
    foreach(GridViewRow gvr in science_tab.Rows)
    {
        if (((CheckBox)gvr.FindControl("sciencefees_check")).Checked == true)
        {
            Response.Write("I am True");//Do Something
        }
        else
        {
            Response.Write("I am False");  //Do Something  //Label1.Text = " unchecked  ";
        }
    }
}


protected void Page_Load(object sender, EventArgs e)
{

//Test Data for binding...
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Col1", typeof(string)));
    dt.Columns.Add(new DataColumn("Col2", typeof(string)));
    for (int i = 0; i <= 10; i++)
    {
        dt.Rows.Add(new string[]{string.Empty, i.ToString()});
    }
    ViewState["CurrentTable"] = dt;

    science_tab.DataSource = dt;
    science_tab.DataBind();
}
于 2013-06-12T06:17:59.430 回答