前言:
已经快半年没有来这里写一写技术文档了,从年后开始,来到一家传统的服装的公司,并开始组建一个新的开发团队,为公司的转型开始做准备。公司的老板看到了淘宝的强大,PPG在以服装行业的身份在电子商务的崛起,时尚起义,凡客成品……..从数码产品到服装行业,电子商务逐渐的融入了每个行业。从组建团队到开始项目设计,到程序开发到现在,已经经历了快3个月了。我们的进度进展的很顺利,一直都在按进度进行着。遇到问题,大家一同讨论解决方案。从美工设计到程序开发,到系统测试,我对现在的团队还算满意。当然,Web开发这个东西其实技术难度是不算很大的,只要业务逻辑上和数据库的设计上没有蛮大的问题。最近一段时间,我的工作是:系统的各个功能设计,与我们的金蝶ERP,进销存BSS系统,支付宝,银联电子支付,99bill快钱的接口开发。同时需要做购物流程,帮助同事解决项目中遇到的问题。毕竟这个项目我其实已经是第二次做了,挑战性不是很大。所以没有很特别让我难忘的技术难题。
正题:
在Repeater中使用RadionButton好象看到的比较少,一般很多都是用的CheckBox.不做不知道,一做才知道两者的区别蛮大的:
昨天想到一个如图的功能: ![blogs0524.JPG](/images/cnblogs_com/teracy/blogs0524.JPG)
![](/WebResource.axd?d=pLXXeGbWF7eXU8SMs2-GFZvUWY2JNH05dFx5YzJhGUYAYJAFEaTEq36NAhTPy7_KekvzDFwt8wvQWdByvJIGWdEq6x2KpKD80&t=633043282340000000)
这个技术的实现,可能有很多方法。我想做一个性能最优的。看到很多人用GridView绑定的数据源,我天生貌似对这个控件比较敏感,不喜欢用它。昨天晚上重写了一个GroupRadioButton, 代码如下:
先重写RadionButton:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
[ToolboxData("<{0}:GroupRadioButton runat=server></{0}:GroupRadioButton>")]
2
public class GroupRadioButton : RadioButton, IPostBackDataHandler
3![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
4
public GroupRadioButton()
5
: base()
6![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
}
8![](/Images/OutliningIndicators/InBlock.gif)
9![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
10![](/Images/OutliningIndicators/InBlock.gif)
11
private string Value
12![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
get
14![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
string val = Attributes["value"];
16
if (val == null)
17
val = UniqueID;
18
else
19
val = UniqueID + "_" + val;
20
return val;
21
}
22
}
23![](/Images/OutliningIndicators/InBlock.gif)
24
#endregion
25![](/Images/OutliningIndicators/InBlock.gif)
26![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Rendering#region Rendering
27![](/Images/OutliningIndicators/InBlock.gif)
28
protected override void Render(HtmlTextWriter output)
29![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
RenderInputTag(output);
31
}
32![](/Images/OutliningIndicators/InBlock.gif)
33
private void RenderInputTag(HtmlTextWriter htw)
34![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/InBlock.gif)
44
string onClick = Attributes["onclick"];
45
if (AutoPostBack)
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
if (onClick != null)
56
htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
57
}
58![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
68
#endregion
69![](/Images/OutliningIndicators/InBlock.gif)
70![](/Images/OutliningIndicators/ContractedSubBlock.gif)
IPostBackDataHandler Members#region IPostBackDataHandler Members
71![](/Images/OutliningIndicators/InBlock.gif)
72
void IPostBackDataHandler.RaisePostDataChangedEvent()
73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
OnCheckedChanged(EventArgs.Empty);
75
}
76![](/Images/OutliningIndicators/InBlock.gif)
77
bool IPostBackDataHandler.LoadPostData(string postDataKey,
78
System.Collections.Specialized.NameValueCollection postCollection)
79![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
bool result = false;
81
string value = postCollection[GroupName];
82
if ((value != null) && (value == Value))
83![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
if (!Checked)
85![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
Checked = true;
87
result = true;
88
}
89
}
90
else
91![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
if (Checked)
93
Checked = false;
94
}
95
return result;
96
}
97![](/Images/OutliningIndicators/InBlock.gif)
98
#endregion
99
}
然后就可以使用到Page里面了:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Page-Code--HTML
<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>
如何得到我们选中的那一项呢:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code---得到选中项
foreach (RepeaterItem item in Rep.Items)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
RadioButton rb = (RadioButton)item.FindControl("GroupRadioButton1");
if (rb.Checked == true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Label lblSysNo = (Label)item.FindControl("lblSysNo");
Label lblName = (Label)item.FindControl("lblName");
Response.Write(lblSysNo.Text.Trim() + lblName.Text.Trim());
![](/Images/OutliningIndicators/InBlock.gif)
}
如果需要默认选中哪一项:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code--设置默认的选中项
protected void Rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
![](/Images/OutliningIndicators/InBlock.gif)
if (dr["SysNo"].ToString() == "91797")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WebA0508.GroupRadioButton grb
= e.Item.FindControl("GroupRadioButton1") as WebA0508.GroupRadioButton;
![](/Images/OutliningIndicators/InBlock.gif)
grb.Checked = true;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
如果需要在ItemDataBound 事件中的用:DataRow dr = ((DataRowView)e.Item.DataItem).Row;,我们的Repeater 控件就只能使用项模版ItemTemplate,其他的模板最好别用。不然就会报错提示:Object reference not set to an instance of an object. 就这样的简单。需要注意的地方也说了。代码说明一切。
字体颜色有点乱,由于时间关系.能和大家分享就行.看完后为我们的四川同胞默哀1分钟吧.