repeater中用单选按钮
repeater中用单选按钮
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
[ToolboxData("<{0}:GroupRadioButton runat=server></{0}:GroupRadioButton>")]
2 public class GroupRadioButton : RadioButton, IPostBackDataHandler
3 {
4 public GroupRadioButton()
5 : base()
6 {
7 }
8
9 Properties#region Properties
10
11 private string Value
12 {
13 get
14 {
15 string val = Attributes["value"];
16 if (val == null)
17 val = UniqueID;
18 else
19 val = UniqueID + "_" + val;
20 return val;
21 }
22 }
23
24 #endregion
25
26 Rendering#region Rendering
27
28 protected override void Render(HtmlTextWriter output)
29 {
30 RenderInputTag(output);
31 }
32
33 private void RenderInputTag(HtmlTextWriter htw)
34 {
35 htw.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
36 htw.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
37 htw.AddAttribute(HtmlTextWriterAttribute.Name, GroupName);
38 htw.AddAttribute(HtmlTextWriterAttribute.Value, Value);
39 if (Checked)
40 htw.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
41 if (!Enabled)
42 htw.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
43
44 string onClick = Attributes["onclick"];
45 if (AutoPostBack)
46 {
47 if (onClick != null)
48 onClick = String.Empty;
49 onClick += Page.GetPostBackClientEvent(this, String.Empty);
50 htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
51 htw.AddAttribute("language", "javascript");
52 }
53 else
54 {
55 if (onClick != null)
56 htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
57 }
58
59 if (AccessKey.Length > 0)
60 htw.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
61 if (TabIndex != 0)
62 htw.AddAttribute(HtmlTextWriterAttribute.Tabindex,
63 TabIndex.ToString(NumberFormatInfo.InvariantInfo));
64 htw.RenderBeginTag(HtmlTextWriterTag.Input);
65 htw.RenderEndTag();
66 }
67
68 #endregion
69
70 IPostBackDataHandler Members#region IPostBackDataHandler Members
71
72 void IPostBackDataHandler.RaisePostDataChangedEvent()
73 {
74 OnCheckedChanged(EventArgs.Empty);
75 }
76
77 bool IPostBackDataHandler.LoadPostData(string postDataKey,
78 System.Collections.Specialized.NameValueCollection postCollection)
79 {
80 bool result = false;
81 string value = postCollection[GroupName];
82 if ((value != null) && (value == Value))
83 {
84 if (!Checked)
85 {
86 Checked = true;
87 result = true;
88 }
89 }
90 else
91 {
92 if (Checked)
93 Checked = false;
94 }
95 return result;
96 }
97
98 #endregion
99 }
然后就可以使用到Page里面了:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<table>
<tr>
<td>
名字</td>
<td>
ID</td>
<td>
EMAIL</td>
<td>
PHONE</td>
</tr>
<asp:Repeater ID="Rep" runat="server" OnItemDataBound="Rep_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<sc1:GroupRadioButton ID="GroupRadioButton1" runat="server" />
<asp:Label ID="lblSysNo" Text='<%# Eval("SysNo") %>' runat="server">
</asp:Label>
</td>
<td>
<asp:Label ID="lblName" Text='<%# Eval("CustomerName") %>' runat="server"></asp:Label>
</td>
<td>
<%# Eval("CustomerID") %>
</td>
<td>
<%# Eval("Email") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
如何得到我们选中的那一项呢:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
foreach (RepeaterItem item in Rep.Items)
{
RadioButton rb = (RadioButton)item.FindControl("GroupRadioButton1");
if (rb.Checked == true)
{
Label lblSysNo = (Label)item.FindControl("lblSysNo");
Label lblName = (Label)item.FindControl("lblName");
Response.Write(lblSysNo.Text.Trim() + lblName.Text.Trim());
}
如果需要默认选中哪一项:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected void Rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
if (dr["SysNo"].ToString() == "91797")
{
WebA0508.GroupRadioButton grb
= e.Item.FindControl("GroupRadioButton1") as WebA0508.GroupRadioButton;
grb.Checked = true;
}
}
如果需要在ItemDataBound 事件中的用:DataRow dr = ((DataRowView)e.Item.DataItem).Row;,我们的Repeater 控件就只能使用项模版ItemTemplate,其他的模板最好别用。不然就会报错提示:Object reference not set to an instance of an object. 就这样的简单。需要注意的地方也说了。代码说明一切
/************************************************/
本博客内容如果是原著都会在标题后加上(原著)字样,未加者多数为转载.
/************************************************/