2 // System.Web.UI.WebControls.DataControlButton.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.ComponentModel;
35 using System.ComponentModel.Design;
38 namespace System.Web.UI.WebControls
40 internal class DataControlButton: Button
44 public DataControlButton (Control container)
46 this.container = container;
47 CausesValidation = false;
50 public DataControlButton (Control container, string text, string image, string command, string commandArg, bool allowCallback)
55 CommandName = command;
56 CommandArgument = commandArg;
57 AllowCallback = allowCallback;
60 public Control Container {
61 get { return container; }
62 set { container = value; }
65 public string ImageUrl {
67 object o = ViewState["iu"];
68 if (o != null) return (string) o;
72 ViewState["iu"] = value;
76 public bool AllowCallback {
78 object o = ViewState["ac"];
79 if (o != null) return (bool) o;
83 ViewState["ac"] = value;
87 public virtual ButtonType ButtonType {
89 object ob = ViewState ["ButtonType"];
90 if (ob != null) return (ButtonType) ob;
91 return ButtonType.Link;
94 ViewState ["ButtonType"] = value;
98 protected internal override void Render (HtmlTextWriter writer)
100 if (CommandName.Length > 0 && ButtonType == ButtonType.Link) {
104 if (CommandName.Length > 0 || CommandArgument.Length > 0 || ButtonType == ButtonType.Button)
106 string postScript = null;
107 string callScript = null;
108 PostBackOptions ops = null;
110 IPostBackContainer pcner = container as IPostBackContainer;
112 ops = pcner.GetPostBackOptions (this);
113 ops.RequiresJavaScriptProtocol = ButtonType == ButtonType.Link;
116 ops = GetPostBackOptions ();
118 postScript = Page.ClientScript.GetPostBackEventReference (ops, !Page.IsCallback);
121 ICallbackContainer ccner = container as ICallbackContainer;
123 callScript = ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
126 ControlStyle.AddAttributesToRender (writer);
128 if (ButtonType == ButtonType.Image) {
129 writer.AddAttribute (HtmlTextWriterAttribute.Type, "image");
130 writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
131 if (callScript != null)
132 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
134 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
136 writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
137 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
138 writer.RenderBeginTag (HtmlTextWriterTag.Input);
139 writer.RenderEndTag ();
141 if (ButtonType == ButtonType.Link) {
142 if (callScript != null) {
143 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
145 writer.AddAttribute (HtmlTextWriterAttribute.Href, postScript);
146 writer.RenderBeginTag (HtmlTextWriterTag.A);
148 writer.RenderEndTag ();
150 if (ButtonType == ButtonType.Button) {
151 if (callScript != null)
152 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
154 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
156 writer.AddAttribute (HtmlTextWriterAttribute.Type, "button");
157 writer.AddAttribute (HtmlTextWriterAttribute.Name, ClientID);
158 writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
159 writer.RenderBeginTag (HtmlTextWriterTag.Input);
160 writer.RenderEndTag ();
164 if (ImageUrl.Length > 0) {
165 ControlStyle.AddAttributesToRender (writer);
166 writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
168 writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
169 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
170 writer.RenderBeginTag (HtmlTextWriterTag.Img);
171 writer.RenderEndTag ();
174 if (!ControlStyle.IsEmpty) {
175 ControlStyle.AddAttributesToRender (writer);
177 writer.RenderBeginTag (HtmlTextWriterTag.Span);
179 writer.RenderEndTag ();
184 protected override PostBackOptions GetPostBackOptions () {
185 PostBackOptions options = new PostBackOptions (this);
186 options.Argument = "";
187 options.RequiresJavaScriptProtocol = ButtonType == ButtonType.Link;
188 options.ClientSubmit = true;
189 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
190 if (options.PerformValidation)
191 options.ValidationGroup = ValidationGroup;
196 private void EnsureForeColor () {
197 if (ForeColor != Color.Empty)
200 for (Control parent = Parent; parent != null; parent = parent.Parent) {
201 WebControl wc = parent as WebControl;
202 if (wc != null && wc.ForeColor != Color.Empty) {
203 ForeColor = wc.ForeColor;
206 if (parent == container)