User Control - Must supply property
Hi guys, i built a user control and i want to make one of its properties as a must, that's mean if you not supply this control when you create the control the project won't be compile, just like if you omit the runat="server" property.
any suggestions ?
Best Regards,
Motil.
Re: User Control - Must supply property
I think you would need to create your own user control class and specify the required parameters in the constructor, that way when it's instantated you have to pass the parameters , if not that should cause a build error.... That's my guess.
http://msdn.microsoft.com/en-us/libr...ercontrol.aspx
Re: User Control - Must supply property
hey,
i think you can pass your property in your class contractor
and if it was null then throw your error.
Re: User Control - Must supply property
couldn't pull it off so far, my best shot so far was to create an interface with the required property and implement it from my user control class but that didn't did the trick also. this is really strange that there is so little information about this over the internet.
anyways i can live without it for now, if i'll ever pull it off I'll make sure to post it here.
Re: User Control - Must supply property
Your right a default constructor is required or you cant inherit your user control from that class :(
I've had the same problem before but obviously didn't solve it, I'm going to do some more thinking on it.
Re: User Control - Must supply property
Quote:
Originally Posted by
motil
couldn't pull it off so far, my best shot so far was to create an interface with the required property and implement it from my user control class but that didn't did the trick also. this is really strange that there is so little information about this over the internet.
anyways i can live without it for now, if i'll ever pull it off I'll make sure to post it here.
hey motil,
can you send us your interface ?
Re: User Control - Must supply property
what about creating a property
Code:
using System;
/// <summary>
/// this class for test
/// </summary>
public class TestClass
{
#region private member(s)
private string _id = null;
#endregion
#region public property(s)
public string ID
{
set
{
_id = value;
}
}
#endregion
public TestClass()
{
string getMyId = ID;
if (getMyId == null)
throw new Exception();
}
}
Re: User Control - Must supply property
what about creating a property such as
Code:
using System;
/// <summary>
/// this class for test
/// </summary>
public class TestClass
{
#region private member(s)
private string _id = null;
#endregion
#region public property(s)
public string ID
{
set
{
_id = value;
}
}
#endregion
public TestClass()
{
string getMyId = ID;
if (getMyId == null)
throw new Exception();
}
}
i don't know may be this can do it, or what you think ?
Re: User Control - Must supply property
avrail - it's very simple interface for testing purpose :
Code:
interface Testing
{
int Test { get; set; }
}
and thanks for the code but what you have there will work (with some modifications) only @ runtime while i want the error will raise @ design time (project will not be able to compile).
thanks again.
Re: User Control - Must supply property
In your interface make your property "Must Have" and I think it will have to implement IControl maybe that will work.... It's alot of trouble to get a required property on a userControl...
Re: User Control - Must supply property
hey motil,
thanks for what, if i was in your position you will help, don't you :D
please after your modification let me see.
Re: User Control - Must supply property
by the way, the class need get {}
sorry.
Re: User Control - Must supply property
Hmmm, I was sure that there would be a way to do this easily, it seems like there is a property attribute for everything else:
http://www.bipinjoshi.net/articles/4...184de5217.aspx
But not whether to indicate that a property is required, which I find a little strange, I am going to have to do some digging.
Gary
Re: User Control - Must supply property
@brin351 how do i set the property as "must have" ?
Re: User Control - Must supply property
Sorry I was on the wrong track I tried creating an interface and implementing it in the usercontrol along with the property in the interface but this did not force a design time need for that property, You already knew that... :(
I tried again with a base class with no default constructor but could not get it --- I think runtime is the only way throw an error unless you reinvent the wheel
Re: User Control - Must supply property
ok brin351 thanks for your efforts!
Re: User Control - Must supply property
Are you designing a User Control or a Server Control?
Re: User Control - Must supply property
User Control ( I'm not sure I know what is server control)