Showing posts with label declaratively. Show all posts
Showing posts with label declaratively. Show all posts

Thursday, March 29, 2012

Set Custom Page properties declaratively

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 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...
>
>

Tuesday, March 13, 2012

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.

set selected value declaratively to dropdownlist

Hi,
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?
I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode
<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />
</EditItemTemplate>
I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any promptsHi,
You can set the selectedvalue property on the dropdownlist as follows
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddl"
DataSourceID="SqlDataSource2"
DataValueField="ProductID"
DataTextField="ProductName" SelectedValue='<%# Bind("ProductID")
%>'></asp:DropDownList>
</EditItemTemplate>
Regards,
Mohamed Mosalem
"podi" wrote:

> Hi,
> I just wonder if there is a way to set a selected item in dropdown list
> which items are retreived from database without touching c# code ?
> I mean, i have in gridview, in edit mode one dropdownlist which
> selected value should be the same as a label text from normal view mode
> <EditItemTemplate>
> <asp:DropDownList ID="ddlGrdvFrequency" runat="server"
> DataTextField="Frequency"
> DataValueField="FrequencyId"
> DataSourceID="SqlDataSourceMembershipFrequency" />
> </EditItemTemplate>
> I was looking for sth like SelectedIndex, but intellisense did not give
> me any prompt for it. I use now C# code in databound event for gridview
> to set this item selected, but ...is there a way of not using this ?
> thanks for any prompts
>
hi podi,
unfortunately the SelectedIndex cannot be set declaratively, it is a compile
error. i'm just wondering how you could know in advance with a databound
list which one to select?
an alternative would be to sort the datasource so that the one you want to
select is the first record, then it will be selected by default when the
list is databound.
i think the databound event is the correct (and best) place to achieve your
task.
i hope this helps
tim
"podi" <lukasz.podolak@.gmail.com> wrote in message
news:1159037422.462578.221520@.d34g2000cwd.googlegroups.com...
> Hi,
> I just wonder if there is a way to set a selected item in dropdown list
> which items are retreived from database without touching c# code ?
> I mean, i have in gridview, in edit mode one dropdownlist which
> selected value should be the same as a label text from normal view mode
> <EditItemTemplate>
> <asp:DropDownList ID="ddlGrdvFrequency" runat="server"
> DataTextField="Frequency"
> DataValueField="FrequencyId"
> DataSourceID="SqlDataSourceMembershipFrequency" />
> </EditItemTemplate>
> I was looking for sth like SelectedIndex, but intellisense did not give
> me any prompt for it. I use now C# code in databound event for gridview
> to set this item selected, but ...is there a way of not using this ?
> thanks for any prompts
>
Thank you for responses, it helped
Tim_Mac wrote:
> hi podi,
> unfortunately the SelectedIndex cannot be set declaratively, it is a compi
le
> error. i'm just wondering how you could know in advance with a databound
> list which one to select?
> an alternative would be to sort the datasource so that the one you want to
> select is the first record, then it will be selected by default when the
> list is databound.
> i think the databound event is the correct (and best) place to achieve you
r
> task.
> i hope this helps
> tim
>
> "podi" <lukasz.podolak@.gmail.com> wrote in message
> news:1159037422.462578.221520@.d34g2000cwd.googlegroups.com...

set selected value declaratively to dropdownlist

Hi,
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?

I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode

<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />

</EditItemTemplate>

I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any promptsHi,
You can set the selectedvalue property on the dropdownlist as follows
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddl"
DataSourceID="SqlDataSource2"
DataValueField="ProductID"
DataTextField="ProductName" SelectedValue='<%# Bind("ProductID")
%>'></asp:DropDownList>
</EditItemTemplate>

Regards,
Mohamed Mosalem

"podi" wrote:

Quote:

Originally Posted by

Hi,
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?
>
I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode
>
<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />
>
</EditItemTemplate>
>
I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any prompts
>
>


hi podi,
unfortunately the SelectedIndex cannot be set declaratively, it is a compile
error. i'm just wondering how you could know in advance with a databound
list which one to select?
an alternative would be to sort the datasource so that the one you want to
select is the first record, then it will be selected by default when the
list is databound.

i think the databound event is the correct (and best) place to achieve your
task.
i hope this helps
tim

"podi" <lukasz.podolak@.gmail.comwrote in message
news:1159037422.462578.221520@.d34g2000cwd.googlegr oups.com...

Quote:

Originally Posted by

Hi,
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?
>
I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode
>
<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />
>
</EditItemTemplate>
>
I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any prompts
>


Thank you for responses, it helped

Tim_Mac wrote:

Quote:

Originally Posted by

hi podi,
unfortunately the SelectedIndex cannot be set declaratively, it is a compile
error. i'm just wondering how you could know in advance with a databound
list which one to select?
an alternative would be to sort the datasource so that the one you want to
select is the first record, then it will be selected by default when the
list is databound.
>
i think the databound event is the correct (and best) place to achieve your
task.
i hope this helps
tim
>
>
>
"podi" <lukasz.podolak@.gmail.comwrote in message
news:1159037422.462578.221520@.d34g2000cwd.googlegr oups.com...

Quote:

Originally Posted by

Hi,
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?

I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode

<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />

</EditItemTemplate>

I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any prompts