Thursday, March 29, 2012
Set Custom Page properties declaratively
I have a base class (which inherits from System.Web.UI.Page) for all the
pages in my application. I have a property defined on this class that I wan
t
to set declaratively in the .aspx (i.e., not in the .aspx.cs) file.
Is there any way to do this declaratively? When I try to use the <@dotnet.itags.org. Page
...> directive, it only allows the predefined attributes to be used.
Thanks,
Richard Brown<script language="CSharp" runat="server">
// put your code in here
</script>
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a horse to water,
but you can't make him think.
"Richard Brown" <Richard Brown@.discussions.microsoft.com> wrote in message
news:70E934C5-8E5D-40C8-ACE0-4A1D2D7587E9@.microsoft.com...
> Hi,
> I have a base class (which inherits from System.Web.UI.Page) for all the
> pages in my application. I have a property defined on this class that I
> want
> to set declaratively in the .aspx (i.e., not in the .aspx.cs) file.
> Is there any way to do this declaratively? When I try to use the <@. Page
> ...> directive, it only allows the predefined attributes to be used.
> Thanks,
> Richard Brown
>
Thanks for replying so quickly. Unfortunately the example you have provided
does not meet my requirements on two counts:
1. This is not declarative. This is code, and I would add this to the
code-behind class.
2. The <script> tags allow the client to define functions, but not run code
.
If the code requires to be run it needs the old-style <% %> breakout.
However this code is then run during the rendering of the page, which is
invoked from the .Net Framework (i.e,. the client does not have control over
when this code is executed).
I was wondering if there is a way of declaratively setting the properties of
the page class before the OnInit (in much the same way that properties are
set in a server control).
Thanks,
Richard Brown
"Kevin Spencer" wrote:
> <script language="CSharp" runat="server">
> // put your code in here
> </script>
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> ..Net Developer
> You can lead a horse to water,
> but you can't make him think.
> "Richard Brown" <Richard Brown@.discussions.microsoft.com> wrote in message
> news:70E934C5-8E5D-40C8-ACE0-4A1D2D7587E9@.microsoft.com...
>
>
Monday, March 26, 2012
Set DIV text in code behind
html:
<div id="divtest" runat=server></div>
code behind:
protected System.Web.UI.HtmlControls.WHAT?? divtest;
...
...
divtest.WHAT?? = "seems to work";Html part is fine. After you've created it, look at it in the design view, save the page, then go to the codebehind, you should be able to see an html div control (some name)... set its innerText property.
hmmm...not so sure
I always have to code the "protected htmlcontrol whatever mycontrol" myself in my project. VS never do it for me. SOMETIMES, VS will add this for me for asp.net controls though.
Maybe something to do with the fact I ALWAYS hand code my pages...never drag and drop onto form...
...aaah...stupid me..let me drag some html controls onto the form and see what VS do for me... sorry..always talk to myself huh?
It's a VS 2003 bug that I discovered much to my consternation when VS 2003 had just come out. Surprisingly, it was addressed by nobody. Everyone thought it was a feature rather than a bug, I even asked MS developers about it, and they did fix it in VS 2005 but they mentioned that very few people ever brought it up regarding VS 2003.
The workaround was that you have to save the ASPX page before going to the codebehind so that the control's code was added to the codebehind file.
Tuesday, March 13, 2012
Set object properties via a loop?
I have a data container object that holds user info, with one public property mapping to each field in my database table.
When retrieving a user's info from my db I'm using a DataReader, and writing code like this:
myDBObj.name = reader["name"];
myDBObj.id = reader["id"];
Etc, etc. This works ok, but I was hoping to use the DataReader's GetName and FieldCount methods to automate setting my property values.
Something like:
for (int i = 0; i <= reader.FieldCount; i++)
{
string fieldName = reader.GetName(i);
usr.[fieldName] = (reader.GetDataTypeName(i))reader[i];
}
The part in bold is throwing me--is there a way to use a variable in determining which property I'm assigning? Is there another way to do what I'm trying?
Thanks!
Aaron
You could try reader[i].ToString()
usr.[fieldName] = reader[i].ToString();
Sample Code:
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand commond = new SqlCommand("SELECT * FROM TEST");
DataSet fillDataSet = new DataSet();
commond.Connection = connection;
commond.CommandType = CommandType.Text;
connection.Open();
SqlDataReader dr;
dr = commond.ExecuteReader();
while(dr.Read())
{
for(int i=0; i<dr.FieldCount;i++)
{
Response.Write(dr.GetName(i) + ":"+ i.ToString() +":" + dr[i].ToString());
Response.Write("<br>");
}
}
connection.Close();
Obtaining the info via the datareader isn't the problem, the issue is that the object I'm creating (objUserDetail) has properties of varying types (strings, ints, datetimes, etc).
While I could certainly simply write out a long list of property assignments and cast the datareader's info in the correct type each time, I'd much rather figure out a way to do it via a loop.
I figured out how to use a PropertyInfo object to get the names of the properties dynamically at runtime, but now the problem is the datareader is sending all the data back as a string, and I can't find a way to convert/cast it into the format the various properties are expecting.
Normally I would do this:
objUserDetail.UserID = (int)reader["UserID"];
But I need a way to convert the reader's field value dynamically.
Anyone know how?
Can you try this..
(dr[i].GetType()) dr[i].ToString()
while(dr.Read())
{
for(int i=0; i<dr.FieldCount;i++)
{
Response.Write(dr.GetName(i) + ":"+ dr[i].GetType() +":" + dr[i].ToString());
Response.Write("<br>");
}
}
Why don't you use TypeDataSet?
like this:
Dim myDBobjAs New usrDim fieldNameAs String Dim flagsAs BindingFlags = BindingFlags.InstanceOr BindingFlags.Public Or BindingFlags.IgnoreCaseOr BindingFlags.Static Or BindingFlags.FlattenHierarchyDim pAs PropertyInfoDim readerAs System.Data.SqlClient.SqlDataReader = GetTheReader()'populate one entityFor iAs Integer = 0To (reader.FieldCount - 1) fieldname = reader.GetName(i) p =GetType(usr).GetProperty(fieldName, flags) p.SetValue(myDBobj, reader(i),Nothing)Next
Thanks for your help, everyone!
I was able to get it working, and now I'm reconsidering the whole data table class concept, since that's what DataSets are for, right?
aaron
DataSets are basically just collections of DataTables.
NC...
Set property default value
I am creating a class where I have various properties.
How to I set a default property value in case the property is not defined by the user.
For example, I have the property:
' Priority
Public Property Priority() As Mail.MailPriority
Get
Return _Priority
End Get
Set(ByVal value As Mail.MailPriority)
_Priority = value
End Set
End Property
I want to set it to Mail.MailPriority.Normal in case the user didn't defined it.
How can I do this?
Thanks,
Miguel
You need to use [DefaultValue(datatype)] for your property definition
For example check thisSet Property Default Value
Thanks
I haven't verified/tested but is the [DefaultValue(datatype)] only used by the Property Browser control ?
An other option is the set your variable in your constructor or in the declaration (Dim a As Integer = 1)
Replace
private _Priority As String
with
private _Priority As String = "Normal"
Set properties on masterpage declaratively
Hi, is it possible to do this?
So you'd set a property on the master page in the aspx.
If by declaratively you mean programmatically, yes you can. Take a look at my example
I created a MasterPage, called BasePage.master, and added the following code to its code-behind:
public partialclass BasePage: System.Web.UI.MasterPage{private int magicNumber;public int MagicNumber {get {return magicNumber; }set { magicNumber =value; } }protected void Page_Load(object sender, EventArgs e) { }} Then I created a new Web Form (.aspx) page, called Test.aspx, and picked Base.master to be its MasterPage. I also added the following line to .aspx page:
<%@. MasterType VirtualPath="~/BasePage.master" %>
Using the MasterType directive allows the .aspx page to access the public properties and fields of the MasterPage via the Master property. The following code is from the code-behind of Test.aspx, take a look:
public partialclass Test : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) { Master.MagicNumber = 2007; }} Hope this helps.
Hi there, I appreciate your effort but since when has doing something declaratively ever meant writing code in a code behind file?? I mean, isn't the whole point of the word 'declarative' to indicate the absence of code?
Hi,
Your original question wasn't clear, as in you didn't say whether or not you had created the property on the Master page or not. Don't scold someone for trying to help you!
If you just need to set an existing property on the master page declaratively, then use the following code (for a string property):
<%Page.Master.MyProperty = "MyValue"%>
Of course though, if the property doesn't already exist on the Master Page, then youMUST write code to create it! For this, refer to the above post for instructions.
Rich
Yes my question was not that clear.
<%Page.Master.MyProperty = "MyValue"%>
The trouble with this is that its a render block so its not going to be set until the end of the page life cycle. I want to set the value of properties on the master page as if the master page was a user control contained by the page and the assignments are within the user control's tag.
Cheers, WT.
Hi,
Now I see what you mean! I don't think there is a way to do this directly. You are gonna have to write some code initially, but you should be able to reuse it on all pages using the same Master Page. The way I would do it would be to create a User Control and:
Expose a public property on the User Control, taking the value of the property you wish to set.