Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating Custom server control. In this control Have created properties for this control. I want create one property with the options value list. This values will be appear in property widow with drop down like border style of any control is appear in property window with the options like NotSet, None, Dotted, Solid...

C#
namespace MessageControl
{
[DefaultProperty("MessageText")]
[ToolboxData("<{0}:MessageControl  runat="server">")]
public class MessageControl : CompositeControl
{

public static string Error = "Error";
public static string Warning = "Warning";
public static string Success = "Success";
public static string Info = "Info";
public static List messagetypeOptions = new List { "Error","Success","Info","Warning"};

Literal ltrMessage = new Literal();

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("Custom MessageControl")]
[Localizable(true)]
public string MessageText
{
get
{
EnsureChildControls();
return ltrMessage.Text != null ? ltrMessage.Text: string.Empty;

}

set
{
ltrMessage.Text = value;

}
}
public enum Options
{
Error,
Success,
Info,
Warning
}

public virtual Options MessageType
{
get;
set;
}




protected override void CreateChildControls()
{
Controls.Clear();
ltrMessage.ID = "ltrMessageText";
this.Controls.Add(ltrMessage);
base.CreateChildControls();
}
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterClientScriptInclude("MessageControl", Page.ClientScript.GetWebResourceUrl(this.GetType(), "MessageControl.css.MessageControlCss.css"));
HtmlGenericControl cssLink = new HtmlGenericControl("link");
cssLink.ID = "MessageControlCssFile";
cssLink.Attributes.Add("href",Page.ClientScript.GetWebResourceUrl(GetType(),"MessageControl.css.MessageControlCss.css"));
cssLink.Attributes.Add("type","text/css");
cssLink.Attributes.Add("rel","StyleSheet");
Page.Header.Controls.Add(cssLink);
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
try
{
string type= MessageType.ToString().ToLower();
switch(type)
{
case "error":
writer.AddAttribute(HtmlTextWriterAttribute.Class, "error");
break;
case "info":
writer.AddAttribute(HtmlTextWriterAttribute.Class, "info");
break;
case "success":
writer.AddAttribute(HtmlTextWriterAttribute.Class, "success");
break;
case "warning":
writer.AddAttribute(HtmlTextWriterAttribute.Class, "warning");
break;
default:
writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#FFF");
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#000");
break;
}
writer.RenderBeginTag(HtmlTextWriterTag.Div);
ltrMessage.RenderControl(writer);
writer.RenderEndTag();
}
catch (Exception ex)
{


}

}
}
}


Sir I am creating a custom server control for message. In this server control i have created one property MessageType and have created enum for the list of values for this property.
But I want list of strings for this property which i will select from dropdown when I use this control. or I can assign the value for this property code behinde(c#).
But by using enum I cant assign string value as MessageType="Error" or MessageType="Success" in cod
Posted
Updated 20-Sep-15 0:18am
v4
Comments
OriginalGriff 20-Sep-15 5:13am    
And?
What have you tried?
Where are you stuck?
What help do you need?

And a better explanation would probably help as well...
Veeshal Mali 20-Sep-15 5:56am    
namespace MessageControl
{
[DefaultProperty("MessageText")]
[ToolboxData("<{0}:MessageControl runat="server">")]
public class MessageControl : CompositeControl
{

public static string Error = "Error";
public static string Warning = "Warning";
public static string Success = "Success";
public static string Info = "Info";
public static List<string> messagetypeOptions = new List<string> { "Error","Success","Info","Warning"};

Literal ltrMessage = new Literal();

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("Custom MessageControl")]
[Localizable(true)]
public string MessageText
{
get
{
EnsureChildControls();
return ltrMessage.Text != null ? ltrMessage.Text: string.Empty;

}

set
{
ltrMessage.Text = value;

}
}
public enum Options
{
Error,
Success,
Info,
Warning
}

public virtual Options MessageType
{
get;
set;
}




protected override void CreateChildControls()
{
Controls.Clear();
ltrMessage.ID = "ltrMessageText";
this.Controls.Add(ltrMessage);
base.CreateChildControls();
}
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterClientScriptInclude("MessageControl", Page.ClientScript.GetWebResourceUrl(this.GetType(), "MessageControl.css.MessageControlCss.css"));
HtmlGenericControl cssLink = new HtmlGenericControl("link");
cssLink.ID = "MessageControlCssFile";
cssLink.Attributes.Add("href",Page.ClientScript.GetWebResourceUrl(GetType(),"MessageControl.css.MessageControlCss.css"));
cssLink.Attributes.Add("type","text/css");
cssLink.Attributes.Add("rel","StyleSheet");
Page.Header.Controls.Add(cssLink);
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
try
{
string type= MessageType.ToString().ToLower();
switch(type)
{
case "error":
writer.AddAttribute(HtmlTextWriterAttribute.Class, "error");
break;
case "info":
writer.AddAttribute(HtmlTextWriterAttribute.Class, "info");
break;
case "success":
writer.AddAttribute(HtmlTextWriterAttribute.Class, "success");
break;
case "warning":
writer.AddAttribute(HtmlTextWriterAttribute.Class, "warning");
break;
default:
writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#FFF");
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#000");
break;
}
writer.RenderBeginTag(HtmlTextWriterTag.Div);
ltrMessage.RenderControl(writer);
writer.RenderEndTag();
}
catch (Exception ex)
{


}

}
}
}
Sir I am creating a custom server control for message. In this server control i have created one property MessageType and have created enum for the list of values for this property.
But I want list of strings for this property which i will select from dropdown when I use this control. or I can assign the value for this property code behinde(c#).
But by using enum I cant assign string value as MessageType="Error" or MessageType="Success" in cod

1 solution

For example:

C#
void Main()
{
        //create instance of Whatever class
	Whatever wt = new Whatever();
        //display default property
	Console.WriteLine("{0}", wt.MessageProperty);
	//set new value for MessageProperty
	wt.MessageProperty = MessageType.Error;
        //display new value
	Console.WriteLine("{0}", wt.MessageProperty);

}

// Enum for MessageType
public enum MessageType
{
	Error,
	Success,
	Question
}

// class which uses Enum as a property
public class Whatever
{
	private MessageType mt;
	
	public Whatever()
	{
		//default MessageProperty value 
		mt = MessageType.Success;
	}

	//here!
	public MessageType MessageProperty
	{
		get{ return mt; }
		set{ mt = value; }
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900