473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom WebContol DropDownList

harshmaul
490 Recognized Expert Contributor
Hi, first let me post my code....

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Text;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8.  
  9. namespace SPGMediaWebControls
  10. {
  11.     [DefaultProperty("Text")]
  12.     [ToolboxData("<{0}:ErrorDropDownList runat=server></{0}:ErrorDropDownList>")]
  13.     public class ErrorDropDownList : DropDownList
  14.     {
  15.         [Bindable(true)]
  16.         [Category("Appearance")]
  17.         [DefaultValue("")]
  18.         [Localizable(true)]
  19.         public string Text
  20.         {
  21.             get
  22.             {
  23.                 String s = (String)ViewState["Text"];
  24.                 return ((s == null) ? String.Empty : s);
  25.             }
  26.  
  27.             set
  28.             {
  29.                 ViewState["Text"] = value;
  30.             }
  31.         }
  32.         private string errorMsg;
  33.  
  34.         public string ErrorMsg
  35.         {
  36.             get { return errorMsg; }
  37.             set { errorMsg = value; }
  38.         }
  39.         public override void RenderEndTag(HtmlTextWriter writer)
  40.         {
  41.             base.RenderEndTag(writer);
  42.             writer.WriteEndTag("div");
  43.         }
  44.         public override void RenderBeginTag(HtmlTextWriter writer)
  45.         {
  46.             errorMsg = errorMsg == null ? "" : errorMsg;
  47.             string errorCSS = "errorCSS";
  48.             if (errorMsg.Length > 1)
  49.             {
  50.                 writer.WriteBeginTag("div class='" + errorCSS + "' style='border:2px solid #0000ff'>");
  51.             }
  52.             else
  53.             {
  54.                 errorMsg = "";
  55.                 writer.WriteBeginTag("div>");
  56.             }
  57.             base.RenderBeginTag(writer);
  58.             writer.Write(errorMsg);
  59.  
  60.         }
  61.         protected override void RenderContents(HtmlTextWriter output)
  62.         {
  63.             output.Write(Text);
  64.         }
  65.     }
  66. }
  67.  
I'm trying to override the drop down list control so that i can standardise validation throughout the all of our user interfaces.

However i can't work out how to add ListItems to this. Some please shoot me, and if you can't do that please tell me what i'm doing wrong.

Thanks.
Feb 21 '08 #1
1 1043
harshmaul
490 Recognized Expert Contributor
Hi all, i solved it, incase your interested.... heres the final code...


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Text;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8.  
  9. namespace MyWebControls
  10. {
  11.     [DefaultProperty("Text")]
  12.     [ToolboxData("<{0}:ErrorDropDownList runat=server></{0}:ErrorDropDownList>")]
  13.     public class ErrorDropDownList : System.Web.UI.WebControls.DropDownList
  14.     {
  15.  
  16.         [
  17.  
  18.         Bindable(true)]
  19.  
  20.         private string errorMsg;
  21.  
  22.         public string ErrorMsg
  23.         {
  24.             get { return errorMsg; }
  25.             set { errorMsg = value; }
  26.         }
  27.         public override void DataBind()
  28.         {
  29.             base.DataBind();
  30.         }
  31.  
  32.         public override void RenderBeginTag(HtmlTextWriter writer)
  33.         {
  34.                         errorMsg = errorMsg == null ? "" : errorMsg;
  35.             string errorCSS = "errorCSS";
  36.             if (errorMsg.Length > 1)
  37.             {
  38.                 writer.WriteBeginTag("div class='" + errorCSS + "' style='border:2px solid #0000ff'>");
  39.             }
  40.             else
  41.             {
  42.                 errorMsg = "";
  43.                 writer.WriteBeginTag("div>");
  44.             }
  45.             base.RenderBeginTag(writer);
  46.         }
  47.         public override void RenderControl(HtmlTextWriter writer)
  48.         {
  49.             base.RenderControl(writer);
  50.         }
  51.         public override void RenderEndTag(HtmlTextWriter writer)
  52.         {
  53.             base.RenderEndTag(writer);
  54.                         writer.Write(errorMsg);
  55.             writer.WriteEndTag("div");
  56.         }
  57.  
  58.     }
  59. }

You can give it a error message and then write a style for the CSS class "errorCSS".
Feb 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1888
by: adam | last post by:
i have custom user control and i'm trying to pass values to custom user control......I need help it seems to me i cannot pass the value to user control from dropdownlist. I have property in a...
0
1496
by: Steve R | last post by:
I've built a composite web custom control with a lot of child controls. I assigned ToolTip values to some of these controls. The ToolTip pop-ups were working fine until I monkeyed with the order...
0
2501
by: RyanG | last post by:
when the value that determines the filter is databound?? I am trying to make a DropDownList for a set of data that I use a lot throughout my project. So I extended the DropDownList to retrieve...
7
3324
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
2
1680
by: Pipo | last post by:
Nobody knows how to get the values provided in the client can be read in the user-control? If have made a Web Custom Control with 2 textboxes and 1 dropdownlist. The user fills in my control (the...
0
1015
by: scott blood | last post by:
Hey all, I have tried posting this question in the relevant Asp.net newsgroups, but have unfortunatly had no reply, so hopefully someone can help me in here. I have created a custom webcontrol...
1
1291
by: RSH | last post by:
I am testing a few concepts in preparation of a project. One of the concepts revolves around a custom dropdownlist class. Basically I am overriding the RenderContents and writing a custom value...
1
1251
by: chandan | last post by:
Hi! Can I manipulate the width/Height of scroll bar on asp Panel webcontol? Thanks, Chandan
3
1680
by: EdisonCPP | last post by:
Hi, I have a DetailsView with a template field with a dropdownlist added to it. If the data coming into that field were put into a textbox, it would look like: Small|Medium|Large|X-Large. I...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.