GridView中如何获取<asp:checkbox>选中的值? 15

<formid="form1"runat="server"><div><asp:GridViewID="GridView1"runat="server"AutoGener... <form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
ShowHeader="False" Width="588px"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="projectID" HeaderText="projectID" InsertVisible="False" SortExpression="projectID" />
<asp:BoundField DataField="projectname" HeaderText="projectname" SortExpression="projectname" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<table width="588px" border="1">
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="我要投票" Width="50%" OnClick="Button1_Click" /></td>
<td>
<asp:Button ID="Button2" runat="server" Text="查看投票" Width="50%" /></td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
怎样在点击button按钮时在后台代码接受checkbox的值 谢谢
展开
 我来答
andyxie249
2012-03-19 · TA获得超过133个赞
知道小有建树答主
回答量:122
采纳率:0%
帮助的人:148万
展开全部
你是要单选吧,可以用radio。改为:
前台:(<asp:CheckBox ID="CheckBox1" runat="server" />这句)
...
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="RadioButtonMarkup" runat="server"></asp:Literal> //修改的地方
</ItemTemplate>
</asp:TemplateField>
...
后台:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Grab a reference to the Literal control
Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");

// Output the markup except for the "checked" attribute
output.Text = string.Format(
@"<input type='radio' name='SelectGroup' " + @"id='RowSelector{0}' value='{0}' />", e.Row.RowIndex);

// See if we need to add the "checked" attribute
if (SelectGroupSelectedIndex == e.Row.RowIndex)
{
output.Text += @" checked='checked' />";
}
}
....
}

private int SelectGroupSelectedIndex
{
get
{
if (string.IsNullOrEmpty(Request.Form["SelectGroup"]))
return -1;
else
return Convert.ToInt32(Request.Form["SelectGroup"]);
}
}

protected void Button1_Click(...)
{
int s = SelectGroupSelectedIndex;
if (s == -1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script type='text/javascript'>alert('未选');", false);
return;
}
else
{
.....
}
}

javascript:
function isSelect() {
var ss = document.getElementsByName('SelectGroup');
for (var i = 0; i < ss.length; i++) {
if (ss[i].checked) {
return true;
}
}
alert('提示:\n\n您还没有选择...');
return false;
}
markdian
2012-03-22 · 超过56用户采纳过TA的回答
知道小有建树答主
回答量:143
采纳率:0%
帮助的人:145万
展开全部
for(int i=0;i<GridView1.Rows.Count;i++)
{
CheckBox cbk = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox ;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zuojiming
2012-03-21 · 超过16用户采纳过TA的回答
知道答主
回答量:69
采纳率:0%
帮助的人:39万
展开全部
for(int i=0;i<GridView1.Rows.Count;i++)
{
CheckBox cbk = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox ;
}
CheckBox 你都有了,取值什么的还不是菜。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式