This is a migrated thread and some comments may be shown as answers.

Call javascript function from custom command instead of ajax post?

6 Answers 322 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Temp
Top achievements
Rank 1
Temp asked on 21 May 2012, 06:08 AM

I know I can use a ClientTemplate as such:

columns.Bound(o=>o.EventId).Title("").ClientTemplate("<input type=\"button\" onclick=\"EventDetail(<#=EventId#>)\" value=\"Details\" class=\"t-button\"/>").Filterable(false).HtmlAttributes(new {style = "text-align:center"});


But I would much rather get the custom command button to do it to enable it to live in the same column as the other commands with the nice width and spacing etc.
I couldn't work out how to do this.

So to clarify i want a custom command to call a javascript function as the above code does, EventDetail(eventId).

Thanks!

6 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 23 May 2012, 05:40 PM
Hello,

Setting a JavaScript function to be called by a custom command is not supported but you can use the OnCommand client-side event to achieve this. For example:
columns.Command(commands =>
{
    // Enable Ajax so that the OnCommand client-side event will be used and set the name
    commands.Custom("MyCommand").Text("Details").Ajax(true);
});
function onCommand(e) {
    // if the custom command has been triggered
    if (e.name == "MyCommand") {
        //prevent the request which will be made by default
        e.preventDefault();    
        var item = e.dataItem;
        EventDetail(item.EventId);     
    }
}


Kind regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Temp
Top achievements
Rank 1
answered on 25 May 2012, 02:43 AM
That's perfect thanks!
0
Jie
Top achievements
Rank 1
answered on 22 Jun 2012, 05:39 PM
I have encountered two problems:

1. If I do not provide .Action("action_name", "controller_name", I got an error as follows:
       "value" cannot be null or empty.
    In our case, we want to call a javascript without going into the controller when user clicks a command button.
2. If I provides the action name, it will call javascript onCommand, however, dataitem is undefined.

Thanks for your help.
0
MTC
Top achievements
Rank 1
answered on 12 Feb 2013, 12:04 PM
Hi ..
I am using RadAjaxManager and RadAjaxLoadingPanel with RadGrid. On grid column i have asp linkbutton and on that link button click i am calling one javascript function..
everything is working fine but on that link button click i am not able to show loading panel and also post back occurs :(
below is my code :

 ajax setting is :
 <telerik:AjaxSetting AjaxControlID="lnkbtnDownload">
                                <UpdatedControls>
                                    <telerik:AjaxUpdatedControl ControlID="rg_Attachments" LoadingPanelID="tlkRadAjaxLoadingPnl" />
                                </UpdatedControls>
                            </telerik:AjaxSetting>
On Grid i have asp link button on  GridTemplateColumn like:

  <telerik:GridTemplateColumn HeaderText="Download">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClientClick="return downloadpdf(id);"
                                            ForeColor="Blue"></asp:LinkButton>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>

and this is my javascript function:
 <script language="javascript" type="text/javascript">
        function downloadpdf(id) {
            var annid = id.replace("lnkDownload", "annId");
            window.location = "DownloadAttachment.ashx?id=" + document.getElementById(annid).value;
            return false;
        }
    </script>


Please help........
0
Daniel
Telerik team
answered on 15 Feb 2013, 12:00 PM
Hello Shweta,

I would recommend that you examine the following help topic:
Download Files with Ajaxified Control

Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Samuel
Top achievements
Rank 1
answered on 28 Feb 2013, 04:12 PM
Hi, I think the reason why the property 'dataItem' is undefined is because your grid is defined with a datasource at server side. I mean, your grid receive a model value instead of having a controller name and an action for querying the server to obtain a datasource. In the case your grid is bind on server-side, the only way to get some information about the row is with the 'row' property.
Tags
Grid
Asked by
Temp
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Temp
Top achievements
Rank 1
Jie
Top achievements
Rank 1
MTC
Top achievements
Rank 1
Daniel
Telerik team
Samuel
Top achievements
Rank 1
Share this question
or