1
using
System;
2 using System.Web.UI;
3 using System.Web.UI.WebControls;
4 using System.Globalization;
5
6 namespace Renyu.Web.UI.WebControls
7 {
8 [ToolboxData( " <{0}:GroupRadioButton runat=server></{0}:GroupRadioButton> " )]
9 public class GroupRadioButton : RadioButton, IPostBackDataHandler
10 {
11 public GroupRadioButton() : base ()
12 {
13 }
14
15 #region Properties
16
17 private string Value
18 {
19 get
20 {
21 string val = Attributes[ " value " ];
22 if (val == null )
23 val = UniqueID;
24 else
25 val = UniqueID + " _ " + val;
26 return val;
27 }
28 }
29
30 #endregion
31
32 #region Rendering
33
34 protected override void Render(HtmlTextWriter output)
35 {
36 RenderInputTag(output);
37 }
38
39 private void RenderInputTag(HtmlTextWriter htw)
40 {
41 htw.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
42 htw.AddAttribute(HtmlTextWriterAttribute.Type, " radio " );
43 htw.AddAttribute(HtmlTextWriterAttribute.Name, GroupName);
44 htw.AddAttribute(HtmlTextWriterAttribute.Value, Value);
45 if (Checked)
46 htw.AddAttribute(HtmlTextWriterAttribute.Checked, " checked " );
47 if ( ! Enabled)
48 htw.AddAttribute(HtmlTextWriterAttribute.Disabled, " disabled " );
49
50 string onClick = Attributes[ " onclick " ];
51 if (AutoPostBack)
52 {
53 if (onClick != null )
54 onClick = String.Empty;
55 onClick += Page.GetPostBackClientEvent( this , String.Empty);
56 htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
57 htw.AddAttribute( " language " , " javascript " );
58 }
59 else
60 {
61 if (onClick != null )
62 htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
63 }
64
65 if (AccessKey.Length > 0 )
66 htw.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
67 if (TabIndex != 0 )
68 htw.AddAttribute(HtmlTextWriterAttribute.Tabindex,
69 TabIndex.ToString(NumberFormatInfo.InvariantInfo));
70 htw.RenderBeginTag(HtmlTextWriterTag.Input);
71 htw.RenderEndTag();
72 }
73
74 #endregion
75
76 #region IPostBackDataHandler Members
77
78 void IPostBackDataHandler.RaisePostDataChangedEvent()
79 {
80 OnCheckedChanged(EventArgs.Empty);
81 }
82
83 bool IPostBackDataHandler.LoadPostData( string postDataKey,
84 System.Collections.Specialized.NameValueCollection postCollection)
85 {
86 bool result = false ;
87 string value = postCollection[GroupName];
88 if ((value != null ) && (value == Value))
89 {
90 if ( ! Checked)
91 {
92 Checked = true ;
93 result = true ;
94 }
95 }
96 else
97 {
98 if (Checked)
99 Checked = false ;
100 }
101 return result;
102 }
103
104 #endregion
105 }
2 using System.Web.UI;
3 using System.Web.UI.WebControls;
4 using System.Globalization;
5
6 namespace Renyu.Web.UI.WebControls
7 {
8 [ToolboxData( " <{0}:GroupRadioButton runat=server></{0}:GroupRadioButton> " )]
9 public class GroupRadioButton : RadioButton, IPostBackDataHandler
10 {
11 public GroupRadioButton() : base ()
12 {
13 }
14
15 #region Properties
16
17 private string Value
18 {
19 get
20 {
21 string val = Attributes[ " value " ];
22 if (val == null )
23 val = UniqueID;
24 else
25 val = UniqueID + " _ " + val;
26 return val;
27 }
28 }
29
30 #endregion
31
32 #region Rendering
33
34 protected override void Render(HtmlTextWriter output)
35 {
36 RenderInputTag(output);
37 }
38
39 private void RenderInputTag(HtmlTextWriter htw)
40 {
41 htw.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
42 htw.AddAttribute(HtmlTextWriterAttribute.Type, " radio " );
43 htw.AddAttribute(HtmlTextWriterAttribute.Name, GroupName);
44 htw.AddAttribute(HtmlTextWriterAttribute.Value, Value);
45 if (Checked)
46 htw.AddAttribute(HtmlTextWriterAttribute.Checked, " checked " );
47 if ( ! Enabled)
48 htw.AddAttribute(HtmlTextWriterAttribute.Disabled, " disabled " );
49
50 string onClick = Attributes[ " onclick " ];
51 if (AutoPostBack)
52 {
53 if (onClick != null )
54 onClick = String.Empty;
55 onClick += Page.GetPostBackClientEvent( this , String.Empty);
56 htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
57 htw.AddAttribute( " language " , " javascript " );
58 }
59 else
60 {
61 if (onClick != null )
62 htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
63 }
64
65 if (AccessKey.Length > 0 )
66 htw.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
67 if (TabIndex != 0 )
68 htw.AddAttribute(HtmlTextWriterAttribute.Tabindex,
69 TabIndex.ToString(NumberFormatInfo.InvariantInfo));
70 htw.RenderBeginTag(HtmlTextWriterTag.Input);
71 htw.RenderEndTag();
72 }
73
74 #endregion
75
76 #region IPostBackDataHandler Members
77
78 void IPostBackDataHandler.RaisePostDataChangedEvent()
79 {
80 OnCheckedChanged(EventArgs.Empty);
81 }
82
83 bool IPostBackDataHandler.LoadPostData( string postDataKey,
84 System.Collections.Specialized.NameValueCollection postCollection)
85 {
86 bool result = false ;
87 string value = postCollection[GroupName];
88 if ((value != null ) && (value == Value))
89 {
90 if ( ! Checked)
91 {
92 Checked = true ;
93 result = true ;
94 }
95 }
96 else
97 {
98 if (Checked)
99 Checked = false ;
100 }
101 return result;
102 }
103
104 #endregion
105 }
106 }
原文摘自:http://www.cnblogs.com/renyu732/archive/2005/12/17/298991.html