获取GridViw中CheckBox的值
<asp:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "False " DataSourceID= "SqlDataSource1 ">
<FooterStyle BackColor= "#990000 " Font-Bold= "true " ForeColor= "white " />
<Columns>
<asp:BoundField DataField= "VoteID " HeaderText= "VoteID " ReadOnly= "true " SortExpression= "VoteID " />
<asp:BoundField DataField= "item " HeaderText= "项目名 " SortExpression= "item " />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat= "server " id= "cbxID " />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor= "#FFFBD6 " ForeColor= "#333333 " HorizontalAlign= "center " />
</asp:GridView>
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ConnectionString= " <%$appSettings:SqlConnnectionString %> " SelectCommand= "Select VoteID,Item From Votes "> </asp:SqlDataSource>
<br />
<asp:Button ID= "btnVote_Click " runat= "server " OnClick= "btnVote_Click_Click " Text= "我要投票 " />
-------------------------这是前后GridView的代码。。
---------------------------------
protected void btnVote_Click_Click(object sender, EventArgs e)
{
try
{
// web Web = new web();
foreach (GridViewRow item in GridView1.Rows)
{
CheckBox check = (CheckBox)item.FindControl( "cbxID ");
if (check != null && check.Checked == true)
{
int ID = Int32.Parse(GridView1.DataKeys[item.RowIndex].ToString());
Response.Write( "ID " + ID + " <br> ");
}
}
}
catch(Exception ex)
{
Response.Write( "asdf ");
}
}
-----------------------这是获取CheckBox的值的代码。。为什么没法获取到呢。
在GridView里面如果用 <asp:CheckBoxFields> 可以把ID值与它绑定吗?
[解决办法]
CheckBox的Text属性可以绑定一个字段,但往往绑定显示的文本,绑定ID的话显示一个数字感觉不舒服。一般要绑定ID且不让ID显示的话,可以在模板中再放一个HiddenField,它的Value属性绑定ID。
[解决办法]
CheckBox check = item.FindControl( "cbxID ") as CheckBox;
[解决办法]
foreach (GridViewItem item in GridView1.Items)
{
CheckBox check = (CheckBox)item.FindControl( "cbxID ");
if (check != null && check.Checked == true)
{
int ID = Int32.Parse(e.cell[0].Text);
Response.Write( "ID " + ID + " <br> ");
}
}
}
[解决办法]
btnVote_Click_Click(object sender, EventArgs e)
你这个方法里本来就没有写取到ID后更新的方法啊,你的问题问的是取不到CKECKBOX的值啊