获取动态生成checkbox的id

sk1078 2012-09-26 04:53:26
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="No.aspx.cs" Inherits="Guest_No"
Title="" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:DropDownList ID="Year" runat="server">
<asp:ListItem Value="0">请选择年</asp:ListItem>
<asp:ListItem>2012</asp:ListItem>
<asp:ListItem>2013</asp:ListItem>
<asp:ListItem>2014</asp:ListItem>
<asp:ListItem>2015</asp:ListItem>
<asp:ListItem>2016</asp:ListItem>
<asp:ListItem>2017</asp:ListItem>
<asp:ListItem>2018</asp:ListItem>
<asp:ListItem>2019</asp:ListItem>
<asp:ListItem>2020</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Month" runat="server">
<asp:ListItem Value="0">请选择月</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<div runat="server" id="div1" visible="false">
<asp:Table ID="Table1" runat="server" BorderColor="Black" BorderWidth="1px" GridLines="Both">
</asp:Table>
<asp:Button ID="Button2" runat="server" Text="提交" OnClick="Button2_Click" /><asp:Button
ID="Button3" runat="server" Text="重置" />
</div>
</asp:Content>
 protected void Button1_Click(object sender, EventArgs e)
{
int i, j, k;
int year = Convert.ToInt32(Year.SelectedValue.ToString());
int month = Convert.ToInt32(Month.SelectedValue.ToString());
if (year.ToString() == "0" || month.ToString() == "0")
{
Label1.Text = "请选择年月";
}
else if (year.ToString() == "0")
{
Label1.Text = "请选择年";
}
else if (month.ToString() == "0")
{
Label1.Text = "请选择月";
}
else
{
int day = Convert.ToInt32(DateTime.DaysInMonth(year, month).ToString());
DateTime firstDay = new DateTime(year, month, 1);
DateTime lostDay = new DateTime(year, month, DateTime.DaysInMonth(year, month));
TableRow tRow1 = new TableRow();
Table1.Rows.Add(tRow1);
TableCell Cell1 = new TableCell();
Cell1.Text = "号";
tRow1.Cells.Add(Cell1);
for (i = 1; i <= day; i++)
{
TableCell tCell1 = new TableCell();
tCell1.Text = i.ToString();
tRow1.Cells.Add(tCell1);
}
TableRow tRow2 = new TableRow();
Table1.Rows.Add(tRow2);
TableCell Cell2 = new TableCell();
Cell2.Text = "1,2";
tRow2.Cells.Add(Cell2);
for (i = 1; i <= day; i++)
{
TableCell tCell2 = new TableCell();
tRow2.Cells.Add(tCell2);
CheckBox CB = new CheckBox();
CB.ID = "cb1" + i.ToString();
tCell2.Controls.Add(CB);
}
}

怎么获取checkbox的id,使用了母板页,这是内容页
...全文
313 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-09-27
  • 打赏
  • 举报
回复
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Form.Count; i++)
{
Response.Write("<li>" + Request.Form.Keys[i] + " = " + Request.Form[i]);
}
}

根据你的Request.Form.Keys[i].IndexOf("cb1")>-1判断是否是cb1
sk1078 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

加不加母板页都会变的

你遍历Request.Form集合就能得到了
[/Quote]求代码
孟子E章 2012-09-27
  • 打赏
  • 举报
回复
加不加母板页都会变的

你遍历Request.Form集合就能得到了

sk1078 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

动态添加的控件,提交的时候最好采用Request.Form得到选择的内容
[/Quote]
使用了母板页,查看生成的源码为<input id="ctl00_ContentPlaceHolder1_cb11" type="checkbox" name="ctl00$ContentPlaceHolder1$cb11" />
Alex20150716 2012-09-27
  • 打赏
  • 举报
回复
前台获取到后,可以传递到后台。
孟子E章 2012-09-27
  • 打赏
  • 举报
回复
动态添加的控件,提交的时候最好采用Request.Form得到选择的内容
孟子E章 2012-09-27
  • 打赏
  • 举报
回复
后台也是CB.ClientID属性啊

服务器端id不就是你写的那个吗
sk1078 2012-09-27
  • 打赏
  • 举报
回复
求大神,求解答
insus 2012-09-27
  • 打赏
  • 举报
回复
sk1078 2012-09-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

用js获取不是更方便吗。。。
[/Quote]
关键是我想用c#后台获取,能实现吗?
yujiajun5 2012-09-26
  • 打赏
  • 举报
回复
用js获取不是更方便吗。。。

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧