How to call context macros related to form from web-part?

Sergey Lebedev asked on March 3, 2014 07:56

Hello, I have a custom web-part, based on Kentico Forms. Inside web part - a have a custom property, which i could fill throught edit mode designer page. When i am trying to use Macro in this field - i am getting empty string.

The exact Macro = "{%MyField%}"

However, in code i can use

 var str = viewBiz.BasicForm.GetFieldValue("MyField") as String;

and everything is fine.

Any ideas how to make Macro work?

Recent Answers


Brenden Kehren answered on March 3, 2014 10:06

Where are you trying to set or get the macro data? When someone is filling out the form? Submitting the form? Typically, you'd set the default value in the form definition and use {%FieldName%} as you're doing now.

0 votesVote for this answer Mark as a Correct answer

Sergey Lebedev answered on March 3, 2014 12:29

For setting i use Kentiko Edit Mode

See screenshot below: Image Text My field is markered.

And getting part, in OnAfterValidate eventhandler

    void viewBiz_OnAfterValidate(object sender, EventArgs e)
    {
        var str = ValidationHelper.GetString(GetValue("RedirectingURL"), "");

        if (!String.IsNullOrEmpty(str))
        {
            viewBiz.BasicForm.StopProcessing = true;
            Response.Redirect(str);
        }
    }

It seems value already been processed by parser. If i write something like {%123%} in RedirectingURL - this code will give me "123" or something.

No diffrence - if i disable macro and parse it manually like:

var redirect = CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros(str);
0 votesVote for this answer Mark as a Correct answer

Richard Sustek answered on March 4, 2014 01:08

Hi,

Thank you for your message.

Can you also show us where exactly are you trying to use this macro? It most likely seems you are using it in a context which is not available in that case you could set the macro to cookie and then get it with cookie macro.

link text

Wouldnt that work?

Kind regards,

Richard Sustek

0 votesVote for this answer Mark as a Correct answer

Sergey Lebedev answered on March 4, 2014 05:21

From the beginning - i wanted to create web-part on form base, which won't save any data and just make a conditional redirect.

I am using propery inside web-part code-behind, if i understood your question correctly. http://pastebin.com/bj0JgDzX

Thanks for the answers - i think i wil dig in that way, but i think, that cookies some kind of a rude solution.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 5, 2014 12:30

You need to define the property in code, then define it in the webpart within the UI (Site Manager>Development>Webparts>your webpart). Then in your event access it via the property name you created in the code behind.

For instance define this property in your webpart code behind:

/// <summary>
/// Redirect URL to use
/// </summary>
public string RedirectUrl
{
    get
    {
        return ValidationHelper.GetString(GetValue("RedirectUrl"), "");
    }
    set
    {
        SetValue("RedirectUrl", value);
    }
}

Then in Site Manager>Development>Webparts>your webpart, add the RedirectUrl as a new string property under the property tab.

Then in your event access it like so:

void viewBiz_OnAfterValidate(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(RedirectUrl))
    {
        viewBiz.BasicForm.StopProcessing = true;
        Response.Redirect(Redirecturl);
    }
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.