昨天做个项目,其中有个页面,Repeater中要嵌套RadioButton和CheckBoxList,参照别人的方法,但就是取不到RadioButtonList的SelectedValue,弄了一个晚上才发现,绑定数据的时候没有放在!IsPostBack中,郁闷。代码如下
aspx
----------------------------------------------------
1
<asp:repeater id="Rt_One" Runat="server">
2
<ItemTemplate>
3
<tr>
4
<td valign="top">
5
<asp:TextBox ID="txtquest" Runat="server" Text='<%# DataBinder.Eval( Container.DataItem ,"[questionid]") %>' Width="0">
6
</asp:TextBox>
7
<asp:Label ID="lblQuest" Runat="server">
8
<%# DataBinder.Eval( Container.DataItem ,"[Num]") %>
9
</asp:Label>
10
:<%# DataBinder.Eval( Container.DataItem ,"[strquestiontitle]") %> [<%# DataBinder.Eval( Container.DataItem ,"[store]") %>分]</td>
11
</tr>
12
<tr>
13
<td>
14
<asp:RadioButtonList ID="rbAnswer" Runat="server"></asp:RadioButtonList></td>
15
</tr>
16
</ItemTemplate>
17
</asp:repeater>
<asp:repeater id="Rt_One" Runat="server">2
<ItemTemplate>3
<tr>4
<td valign="top">5
<asp:TextBox ID="txtquest" Runat="server" Text='<%# DataBinder.Eval( Container.DataItem ,"[questionid]") %>' Width="0">6
</asp:TextBox>7
<asp:Label ID="lblQuest" Runat="server">8
<%# DataBinder.Eval( Container.DataItem ,"[Num]") %>9
</asp:Label>10
:<%# DataBinder.Eval( Container.DataItem ,"[strquestiontitle]") %> [<%# DataBinder.Eval( Container.DataItem ,"[store]") %>分]</td>11
</tr>12
<tr>13
<td>14
<asp:RadioButtonList ID="rbAnswer" Runat="server"></asp:RadioButtonList></td>15
</tr>16
</ItemTemplate>17
</asp:repeater> 1
<asp:repeater id="Rt_More" Runat="server">
2
<ItemTemplate>
3
<tr>
4
<td valign="top">
5
<asp:TextBox ID="txtQuest2" Runat="server" Text='<%# DataBinder.Eval( Container.DataItem ,"[questionid]") %>' Width="0">
6
</asp:TextBox>
7
<asp:Label ID="lblQuest2" Runat="server">
8
<%# DataBinder.Eval( Container.DataItem ,"[Num1]") %>
9
</asp:Label>:<%# DataBinder.Eval( Container.DataItem ,"[strquestiontitle]") %> [<%# DataBinder.Eval( Container.DataItem ,"[store]") %>分]</td>
10
</tr>
11
<tr>
12
<td colspan="2">
13
<asp:CheckBoxList ID="chkAnswer" Runat="server"></asp:CheckBoxList></td>
14
</tr>
15
</ItemTemplate>
16
</asp:repeater>
<asp:repeater id="Rt_More" Runat="server">2
<ItemTemplate>3
<tr>4
<td valign="top">5
<asp:TextBox ID="txtQuest2" Runat="server" Text='<%# DataBinder.Eval( Container.DataItem ,"[questionid]") %>' Width="0">6
</asp:TextBox>7
<asp:Label ID="lblQuest2" Runat="server">8
<%# DataBinder.Eval( Container.DataItem ,"[Num1]") %>9
</asp:Label>:<%# DataBinder.Eval( Container.DataItem ,"[strquestiontitle]") %> [<%# DataBinder.Eval( Container.DataItem ,"[store]") %>分]</td>10
</tr>11
<tr>12
<td colspan="2">13
<asp:CheckBoxList ID="chkAnswer" Runat="server"></asp:CheckBoxList></td>14
</tr>15
</ItemTemplate>16
</asp:repeater>cs
1
private void Page_Load(object sender, System.EventArgs e)
2
{
3
// 在此处放置用户代码以初始化页面
4
if(!IsPostBack)
5
{
6
RtRt_OneBind();
7
Rt_MoreBind();
8
}
9
}
private void Page_Load(object sender, System.EventArgs e)2
{3
// 在此处放置用户代码以初始化页面4
if(!IsPostBack)5
{6
RtRt_OneBind();7
Rt_MoreBind();8
}9
} 1
private void RtRt_OneBind()
2
{
3
SqlConnection conn=new SqlConnection(strconn);
4
SqlCommand cmd=new SqlCommand();
5
cmd.Connection=conn;
6
conn.Open();
7
cmd.CommandType=CommandType.StoredProcedure;
8
cmd.CommandText="s_TestPageQuestion_GetByPage";
9
cmd.Parameters.Add("@id",SqlDbType.Int).Value=System.Convert.ToInt32(Request.QueryString["id"]);
10
cmd.Parameters.Add("@type",SqlDbType.Int).Value=0;
11
ds=new DataSet();
12
da=new SqlDataAdapter(cmd);
13
da.Fill(ds,"s_TestPageQuestion_GetByPage");
14
Rt_One.DataSource=ds.Tables["s_TestPageQuestion_GetByPage"].DefaultView;
15
Rt_One.DataBind();
16
}
private void RtRt_OneBind()2
{3
SqlConnection conn=new SqlConnection(strconn);4
SqlCommand cmd=new SqlCommand();5
cmd.Connection=conn;6
conn.Open();7
cmd.CommandType=CommandType.StoredProcedure;8
cmd.CommandText="s_TestPageQuestion_GetByPage";9
cmd.Parameters.Add("@id",SqlDbType.Int).Value=System.Convert.ToInt32(Request.QueryString["id"]);10
cmd.Parameters.Add("@type",SqlDbType.Int).Value=0;11
ds=new DataSet();12
da=new SqlDataAdapter(cmd);13
da.Fill(ds,"s_TestPageQuestion_GetByPage");14
Rt_One.DataSource=ds.Tables["s_TestPageQuestion_GetByPage"].DefaultView;15
Rt_One.DataBind();16
} 1
private void Rt_MoreBind()
2
{
3
SqlConnection conn=new SqlConnection(strconn);
4
SqlCommand cmd=new SqlCommand();
5
cmd.Connection=conn;
6
conn.Open();
7
cmd.CommandType=CommandType.StoredProcedure;
8
cmd.CommandText="s_TestPageQuestion_GetByPage";
9
cmd.Parameters.Add("@id",SqlDbType.Int).Value=System.Convert.ToInt32(Request.QueryString["id"]);
10
cmd.Parameters.Add("@type",SqlDbType.Int).Value=0;
11
ds=new DataSet();
12
da=new SqlDataAdapter(cmd);
13
da.Fill(ds,"s_TestPageQuestion_GetByPage");
14
Rt_More.DataSource=ds.Tables["s_TestPageQuestion_GetByPage"].DefaultView;
15
Rt_More.DataBind();
16
}
private void Rt_MoreBind()2
{3
SqlConnection conn=new SqlConnection(strconn);4
SqlCommand cmd=new SqlCommand();5
cmd.Connection=conn;6
conn.Open();7
cmd.CommandType=CommandType.StoredProcedure;8
cmd.CommandText="s_TestPageQuestion_GetByPage";9
cmd.Parameters.Add("@id",SqlDbType.Int).Value=System.Convert.ToInt32(Request.QueryString["id"]);10
cmd.Parameters.Add("@type",SqlDbType.Int).Value=0;11
ds=new DataSet();12
da=new SqlDataAdapter(cmd);13
da.Fill(ds,"s_TestPageQuestion_GetByPage");14
Rt_More.DataSource=ds.Tables["s_TestPageQuestion_GetByPage"].DefaultView;15
Rt_More.DataBind();16
} 1
override protected void OnInit(EventArgs e)
2
{
3
//
4
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
5
//
6
InitializeComponent();
7
Rt_One.ItemDataBound +=new RepeaterItemEventHandler(Rt_One_ItemDataBound);
8
Rt_More.ItemDataBound +=new RepeaterItemEventHandler(Rt_More_ItemDataBound);
9
base.OnInit(e);
10
}
override protected void OnInit(EventArgs e)2
{3
//4
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。5
//6
InitializeComponent();7
Rt_One.ItemDataBound +=new RepeaterItemEventHandler(Rt_One_ItemDataBound);8
Rt_More.ItemDataBound +=new RepeaterItemEventHandler(Rt_More_ItemDataBound);9
base.OnInit(e);10
} 1
private void Rt_One_ItemDataBound(object sender, RepeaterItemEventArgs e)
2
{
3
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
4
{
5
RadioButtonList rbl = (RadioButtonList) e.Item.FindControl("rbAnswer");
6
DataRowView row = (DataRowView) e.Item.DataItem;
7
string questionid = row["questionid"].ToString() ;
8
DataSet ands1 = new DataSet( );
9
string sql = "S_Answer_Select";
10
SqlConnection conn=new SqlConnection(strconn);
11
SqlCommand ancmd = new SqlCommand( sql,conn);
12
ancmd.CommandType = CommandType.StoredProcedure;
13
ancmd.Parameters.Add("@questionid", SqlDbType.Int, 4).Value = questionid;
14
SqlDataAdapter anda = new SqlDataAdapter( ancmd);
15
anda.Fill(ands1);
16
if(ands1!=null && ands1.Tables [0].Rows .Count >0)
17
{
18
rbl.DataSource = ands1;
19
rbl.DataTextField = "result";
20
rbl.DataValueField = "id";
21
rbl.DataBind();
22
23
}
24
25
}
26
}
private void Rt_One_ItemDataBound(object sender, RepeaterItemEventArgs e)2
{3
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)4
{5
RadioButtonList rbl = (RadioButtonList) e.Item.FindControl("rbAnswer");6
DataRowView row = (DataRowView) e.Item.DataItem;7
string questionid = row["questionid"].ToString() ;8
DataSet ands1 = new DataSet( );9
string sql = "S_Answer_Select";10
SqlConnection conn=new SqlConnection(strconn);11
SqlCommand ancmd = new SqlCommand( sql,conn);12
ancmd.CommandType = CommandType.StoredProcedure;13
ancmd.Parameters.Add("@questionid", SqlDbType.Int, 4).Value = questionid;14
SqlDataAdapter anda = new SqlDataAdapter( ancmd);15
anda.Fill(ands1);16
if(ands1!=null && ands1.Tables [0].Rows .Count >0)17
{18
rbl.DataSource = ands1;19
rbl.DataTextField = "result";20
rbl.DataValueField = "id";21
rbl.DataBind(); 22
23
}24
25
} 26
} 1
private void Rt_More_ItemDataBound(object sender, RepeaterItemEventArgs e)
2
{
3
CheckBoxList chk = (CheckBoxList) e.Item.FindControl("chkAnswer");
4
DataRowView row = (DataRowView) e.Item.DataItem;
5
string questionid = row["questionid"].ToString() ;
6
DataSet ands = new DataSet( );
7
string sql = "S_Answer_Select";
8
SqlConnection conn=new SqlConnection(strconn);
9
SqlCommand ancmd = new SqlCommand( sql,conn);
10
ancmd.CommandType = CommandType.StoredProcedure;
11
ancmd.Parameters.Add("@questionid", SqlDbType.Int, 4).Value = questionid;
12
SqlDataAdapter anda = new SqlDataAdapter( ancmd);
13
anda.Fill(ands);
14
if(ands!=null && ands.Tables [0].Rows .Count >0)
15
{
16
chk.DataSource = ands;
17
chk.DataTextField = "result";
18
chk.DataValueField = "id";
19
chk.DataBind();
20
}
21
}
private void Rt_More_ItemDataBound(object sender, RepeaterItemEventArgs e)2
{3
CheckBoxList chk = (CheckBoxList) e.Item.FindControl("chkAnswer");4
DataRowView row = (DataRowView) e.Item.DataItem;5
string questionid = row["questionid"].ToString() ;6
DataSet ands = new DataSet( );7
string sql = "S_Answer_Select";8
SqlConnection conn=new SqlConnection(strconn);9
SqlCommand ancmd = new SqlCommand( sql,conn);10
ancmd.CommandType = CommandType.StoredProcedure;11
ancmd.Parameters.Add("@questionid", SqlDbType.Int, 4).Value = questionid;12
SqlDataAdapter anda = new SqlDataAdapter( ancmd);13
anda.Fill(ands);14
if(ands!=null && ands.Tables [0].Rows .Count >0)15
{16
chk.DataSource = ands;17
chk.DataTextField = "result";18
chk.DataValueField = "id";19
chk.DataBind(); 20
}21
} 1
private void Button1_Click(object sender, System.EventArgs e)
2
{
3
4
SqlConnection conn=new SqlConnection(strconn);
5
SqlCommand cmd=new SqlCommand();
6
cmd.Connection=conn;
7
conn.Open();
8
sgring msg="";
9
10
for(int i=0;i<Rt_One .Items .Count;i++)
11
{
12
TextBox questid = (TextBox) Rt_One.Items[i].FindControl("txtquest");
13
RadioButtonList rbl = (RadioButtonList) Rt_One.Items[i].FindControl("rbAnswer");
14
15
if(rbl!=null && rbl.SelectedValue !="")
16
{
17
int questionid = Convert.ToInt32(questid.Text );
18
msg += rbl.SelectedValue.ToString ()+"|";
19
20
}
21
}
22
23
for(int m=0;m<Rt_More .Items .Count ;m++)
24
{
25
string msg = "";
26
CheckBoxList mybox=(CheckBoxList)Rt_More.Items[m].FindControl("chkAnswer");
27
TextBox questid2 = (TextBox) Rt_More.Items[m].FindControl("txtQuest2");
28
int questid2v = Convert.ToInt32(questid2.Text );
29
SqlCommand chkcmd = new SqlCommand( "s_Answer_GetRight",conn);
30
31
if(mybox!=null)
32
{
33
for(int n=0;n<mybox.Items .Count;n++)
34
{
35
if(mybox.Items [n].Selected )
36
{
37
msg += mybox.Items[n].Value +"|";
38
}
39
}
40
}
41
}
42
43
44
}
private void Button1_Click(object sender, System.EventArgs e)2
{3
4
SqlConnection conn=new SqlConnection(strconn);5
SqlCommand cmd=new SqlCommand();6
cmd.Connection=conn;7
conn.Open();8
sgring msg="";9
10
for(int i=0;i<Rt_One .Items .Count;i++)11
{12
TextBox questid = (TextBox) Rt_One.Items[i].FindControl("txtquest");13
RadioButtonList rbl = (RadioButtonList) Rt_One.Items[i].FindControl("rbAnswer");14
15
if(rbl!=null && rbl.SelectedValue !="")16
{17
int questionid = Convert.ToInt32(questid.Text );18
msg += rbl.SelectedValue.ToString ()+"|";19
20
}21
}22
23
for(int m=0;m<Rt_More .Items .Count ;m++)24
{25
string msg = "";26
CheckBoxList mybox=(CheckBoxList)Rt_More.Items[m].FindControl("chkAnswer");27
TextBox questid2 = (TextBox) Rt_More.Items[m].FindControl("txtQuest2");28
int questid2v = Convert.ToInt32(questid2.Text );29
SqlCommand chkcmd = new SqlCommand( "s_Answer_GetRight",conn);30
31
if(mybox!=null)32
{33
for(int n=0;n<mybox.Items .Count;n++)34
{35
if(mybox.Items [n].Selected )36
{37
msg += mybox.Items[n].Value +"|";38
}39
}40
}41
}42

43

44
}
