You are not logged in.

#1 12 Feb 2008 4:46 am

azzlack
Senior Member
From: Bergen, Norway
Registered: Jan 2008
Posts: 26
Website

Adding imagebuttons to web usercontrol at runtime

Is there a way to create unique imagebuttons in a web usercontrol at runtime?

I have this code:

Code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LanguageSelector.ascx.cs" Inherits="LanguageSelector.LanguageSelector" %>

<script runat="server">
    private string _Name, _Redirect;

    public object Name
    {
        get { return _Name; }
        set { _Name = value; }
    }
    
    public object Redirect
    {
        get { return _Redirect; }
        set { _Redirect = value; }
    }
    
</script>

<asp:ImageButton ID="Name" runat="server" onclick="Name_Click"/>

protected void Name_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(umbraco.library.NiceUrl(Redirect));
    }

There is a macro which has these variables on the page. It passes these variables to the usercontrol.

This is what is entered on the page:

Code:

<?ASPNET_FORM>
    <?UMBRACO_MACRO macroAlias="LanguageSelector" Name="English" Redirect="1070"></?UMBRACO_MACRO>
</?ASPNET_FORM>

What I want to happen is that the usercontrol counts how many of these macros that are on the page, and creates imagebuttons for every one of them and the corresponding onclick event.

Last edited by azzlack (12 Feb 2008 4:47 am)


Web Developer and Designer.
Currently studying for Bachelor of Computer Science degree ...

Offline

 

#2 12 Feb 2008 8:23 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Adding imagebuttons to web usercontrol at runtime

I'm not familiar with umbraco and how it lives inside the asp.net page life cycle.  It seems to me that fairly early on, umbraco pre-parses the page and injects something into it based on these macros.  "Macros" are not standard in asp.net at all.  I'm sure the only reason they do this is because its a CMS and they want to support a scripted type of page (even though asp.net pages are compiled) Whether or not the macro even exists at the time your user controls are built up would be what I'd question.

the only way you'd be able to pre-parse the page when requested, would be to handle the requests with your own custom http module (this way you can get the page that was requested, then parse the file for the macro, then pass along the info needed to create the image buttons to the page itself as added get or post parameters). 

there may be another way to do it that umbraco may be able to help you with better than I would (seeing as how I've never used it).

Offline

 

#3 13 Feb 2008 12:07 am

azzlack
Senior Member
From: Bergen, Norway
Registered: Jan 2008
Posts: 26
Website

Re: Adding imagebuttons to web usercontrol at runtime

The usercontrol is built on the information that the "macro" (dunno why they call it a macro, since it only provides an easy way to pass information to the usercontrol) has, so I think it has to exist when the usercontrol is built.

I have also been thinking (going to sleep really puts your brain to work lol) and I think that just creating multiple instances of the usercontrol is the best thing to do. So the usercontrol only contains a single imagebutton, with name and url for the page.

The next level is then to make the users choice into a cookie.
I am only starting to learn programming, and I have never touched cookies before. I know how they work, but not how to make them work... smile

How can I store the name of the imagebutton to a cookie, and make the page check for this cookie on load, and then pass the user to the corresponding page based on what he chose before?


Web Developer and Designer.
Currently studying for Bachelor of Computer Science degree ...

Offline

 

#4 13 Feb 2008 7:46 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Adding imagebuttons to web usercontrol at runtime

the response object has a cookies collection (an array of cookies indexed by name) that allows you to get and set cookies.

Code:

HttpCookie cookie = new HttpCookie("Foo");
cookie.Value = "imageButtonName";
// set whatever other parameters on the cookie you want
// like expire time, whether its secure or not, whether its
// only accessible by HTTP (no scripts can access it) or
// whatever
Response.Cookies.Add(cookie);

// ...

string imageButtonName = Response.Cookies["Foo"]

though if you're only using this to store the info between pages, you can use the viewstate to do the same thing:

Code:

ViewState["Foo"] = "imageButtonName";

string imageButtonName = (string)ViewState["Foo"]

if the macro's controls are built before the rest of the page is processed by the HttpRuntime (which I'd assume it is) then you can also loop through the page's Controls collection and look for the controls added by the macro.

Code:

foreach(Control control in Controls) {
    if(control is WhateverTypeTheMacroBecomes && control.ID == "SomeID") {
        // create an image button based on it?
    }
}

Offline

 

#5 20 Feb 2008 6:02 am

azzlack
Senior Member
From: Bergen, Norway
Registered: Jan 2008
Posts: 26
Website

Re: Adding imagebuttons to web usercontrol at runtime

Here is how the macro stuff works smile :
I kept wondering about it myself, and I finally found out how... big_smile

From Umbraco:

Communicating through Elements and Public Properties
By creating Public Properties in your control and match them with macro elements,
youíre able to communicate with your control directly from within umbraco.
umbraco will even create the UI when inserting the macro. Itís important that the
Alias of your macro element matches the name of your Public Property and be aware
of case-sensitivity!


Web Developer and Designer.
Currently studying for Bachelor of Computer Science degree ...

Offline

 

#6 20 Feb 2008 6:14 am

azzlack
Senior Member
From: Bergen, Norway
Registered: Jan 2008
Posts: 26
Website

Re: Adding imagebuttons to web usercontrol at runtime

Now, to the problem at hand.

Thanks for the heads-up on the cookie stuff, btw... smile

This is where I stand right now:

Code:

public partial class LanguageSelector : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnLang.UniqueID = Name;
            btnLang.ImageUrl = Redirect;
        }

        protected void btnLang_Click(object sender, ImageClickEventArgs e)
        {
            HttpCookie cookie = new HttpCookie("EyeCatch_Language");
            cookie.Value = btnLang.UniqueID;

            Response.Cookies.Add(cookie);
        }
    }

What I wanted to do with this page, was to check if the user has the cookie on Page_Load, and if so, redirect to the corresponding url, given by the ImageUrl property.

But will it then check for the cookie for each control that is on the page?


Web Developer and Designer.
Currently studying for Bachelor of Computer Science degree ...

Offline

 

#7 20 Feb 2008 7:57 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Adding imagebuttons to web usercontrol at runtime

not exactly sure on some of the implementation details there.  does your name and redirect properties check the "EyeCatch_Language" cookie for the information they return?

(if so) the only controls that will check that cookie are the ones you explicitly use to check.

inside your page_load method you can put your assignment inside an if(IsPostBack) { ... } condition since the data for the cookie is set as a condition of a posted back event when the user clicks the lang button (the initial load may not have the cookie info).

Offline

 

#8 10 Mar 2008 3:24 am

azzlack
Senior Member
From: Bergen, Norway
Registered: Jan 2008
Posts: 26
Website

Re: Adding imagebuttons to web usercontrol at runtime

As you probably understand, I have very limited knowledge of C# and how it works with the web.

I know so little that I decided to make the whole thing in Javascript and XSLT instead, and then inserting it multiple times on the page.

(I really need to start learning C#... big_smile)


Web Developer and Designer.
Currently studying for Bachelor of Computer Science degree ...

Offline

 



© 2003 - 2017 NullFX
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License