<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>
以及标签和按钮..
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
这是on按钮onclick事件:
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 ";
}
}
无论我检查多少个复选框,如果语句始终为false ... label1.Text给出结果0未选中1未选中2未选中...
答案 0 :(得分: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();
}