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的值 谢谢 展开
<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的值 谢谢 展开
展开全部
你是要单选吧,可以用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;
}
前台:(<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;
}
展开全部
for(int i=0;i<GridView1.Rows.Count;i++)
{
CheckBox cbk = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox ;
}
{
CheckBox cbk = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox ;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
for(int i=0;i<GridView1.Rows.Count;i++)
{
CheckBox cbk = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox ;
}
CheckBox 你都有了,取值什么的还不是菜。
{
CheckBox cbk = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox ;
}
CheckBox 你都有了,取值什么的还不是菜。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询