Developer forum

Forum » Development » The Toolbar control - server-side clicks

The Toolbar control - server-side clicks

Diogo Lino
Reply
Hello,

I'm trying to use the Toolbar control and in the ToolbarButton there is only available the OnClientClick attribute.

Is there a way to put the buttons of this control doing the same as the RibbonBarButton where we can enable server click?

 

Or any other approach to be able to run some code on a Toolbar button click, to save a form data, for instance?



ToolbarButton:

<dw:ToolbarButton ID="saveButton" runat="server" Text="Guardar" Translate="true" Image="Save" OnClientClick="saveSettings()">

 

RibbonBarButton:

<dw:RibbonBarButton runat="server" Size="Small" Image="Save" ID="saveButton" Title="Guardar alterações" EnableServerClick="True" OnClick="saveButton_Click" />

 

Thanks,
Diogo Lino


Replies

 
Pavel Volgarev
Reply
This post has been marked as an answer
Hi Diogo,

Toolbar control doesn't natively support server-side Click events for its buttons but it's very easy to overcome this limitation by using a hidden Button control in conjunction with ClientScriptManager.GetPostBackEventReference:

Markup:
<dw:ToolbarButton ID="saveButton" Text="Guardar" 
    Translate="true" Image="Save" runat="server" />

<div style="display: none">
    <asp:Button ID="cmdSave" OnClick="cmdSave_Click" runat="server" />
</div>
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
    saveButton.OnClientClick = 
        Page.ClientScript.GetPostBackEventReference(cmdSave, string.Empty);
}

protected void cmdSave_Click(object sender, EventArgs e)
{
    // Handle "Save" action
}
You can also make it work without a hidden button:
protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        // Handle postback ("Save" action)
    }

    saveButton.OnClientClick = 
        Page.ClientScript.GetPostBackEventReference(saveButton, string.Empty);
}
-- Pavel

Votes for this answer: 0
 
Diogo Lino
Reply

Thanks! It worked fine!


I've also made extra code, without the hidden button, to work with multiple buttons:
 

     if (Page.IsPostBack)
     {
                string buttonClicked = Request.Params.Get("__EVENTTARGET");

                switch (buttonClicked)
                {
                    case "saveButton":
                        SaveValues();
                        break;
                    case "saveAndExitButton":
                        SaveValues();
                        Response.Redirect("Default.aspx");
                        break;
                    case "cancelButton":
                        Response.Redirect("Default.aspx");
                        break;
                }
     }

--

Diogo Lino

 

You must be logged in to post in the forum