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

LinkButton passing variables

4 Answers 244 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Konstantinos
Top achievements
Rank 1
Konstantinos asked on 20 Apr 2012, 01:09 PM
Ok, first of all hi :)

ALready programming two-months with Telerik, I feel I started to get the grip of things. However there is something that has troubled me for the past few days in my work trying different implementations, but I still cannot seem to find a solution....

What I want to do:
I want to redirect to a new page through a button ( - I don't know if there are practical differences between Image and Link button other than how it is viewed on the user, but I prefer ImageButton - ), which also will pass a variable it in the url (http://www.lala.com?var=myvar - any other way are acceptable)

What I currently do:
In my .aspx file I have
(Grid Properties)
<
 
telerik:RadGrid ID="rdEthnologicalImages" runat="server"
 
AllowSorting="True" AutoGenerateColumns="False" CellSpacing="0"
 
AllowPaging="True" ShowStatusBar="True" GridLines="None"
 
OnItemCommand="RadGrid_Command" OnNeedDataSource="RadGrid_NeedDataSource"
 
OnItemDataBound="RadGrid_ItemDataBound">

(Grid Buttons - Download is the button to redirect, Link keeps the text to parse)
<telerik:GridButtonColumn
    FilterControlAltText="Filter Download column" HeaderText="Download"
    ImageUrl="~/Img/download.png" ItemStyle-HorizontalAlign="Center" Text="Download"
    CommandName="Download" UniqueName="Download"><HeaderStyle Width="50px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="Link"
        FilterControlAltText="Filter ImageLink column" HeaderText="Link"
        UniqueName="ImageLink" Visible="False">
</telerik:GridBoundColumn>

In my .cs file
(I eventually found out that LinkButton has no OnCLick attribute, so that brings me at a dead end...I think..)
(After it found the correct element, it should call the javascript function in the MasterPage - included in the <head>)
if (e.Item.ItemType == GridItemType.Item)
            {
                GridDataItem dataItem = (GridDataItem)rdEthnologicalImages.MasterTableView.GetItems(GridItemType.Item)[0];
                //LinkButton downloadButton = (LinkButton)dataItem.FindControl("Download");
                LinkButton downloadButton = (LinkButton)e.Item.FindControl("Download");
                if (downloadButton != null)
                {
                    downloadButton.Attributes.Add("OnClick", "popup");
                }
            }


In my .js file
(Since I have the text I need in a hidden column, I get it and pass it to the page I want)

function popup()
        {
 
            var grid = $find("<%=rdEthnologicalImages.ClientID %>");
            var MasterTable = grid.get_masterTableView();
            var selectedRows = MasterTable.get_selectedItems();
 
            for (var i = 0; i < selectedRows.length; i++)
            {
                var row = selectedRows[i];
                var link = MasterTable.getCellByColumnUniqueName(row, "ImageLink");
                window.open("~/DownloadPopup.aspx?filename=link");
                break;
            }
        }


Errors:
My main error is that downloadbutton(.cs) is always null. And even if it progressed properly I already have a bad feeling it won't work even then.

For some parts, I am not even 100% of what their functions are.
Any help in the matter?

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 20 Apr 2012, 01:24 PM
Hello,

Try accessing the buttoncolumn as shown below and then pass the variables.
aspx:
<telerik:GridButtonColumn ButtonType="ImageButton" FilterControlAltText="Filter Download column" HeaderText="Download"
    ImageUrl="~/Img/download.png" ItemStyle-HorizontalAlign="Center" Text="Download"
    CommandName="Download" UniqueName="Download"><HeaderStyle Width="50px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</telerik:GridButtonColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
  GridDataItem item = (GridDataItem)e.Item;
  ImageButton btn = (ImageButton)item["Download"].Controls[0];
  btn.Attributes.Add("OnClick", "popup");
 }
}

Thanks,
Shinu.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 20 Apr 2012, 01:38 PM
Hello,

<telerik:GridButtonColumn FilterControlAltText="Filter Download column" HeaderText="Download"
                       ImageUrl="~/Img/download.png" ItemStyle-HorizontalAlign="Center"
                       CommandName="Download" UniqueName="Download" ButtonType="ImageButton">
                       <HeaderStyle Width="50px"></HeaderStyle>
                       <ItemStyle HorizontalAlign="Center"></ItemStyle>
                   </telerik:GridButtonColumn>
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
 
            GridDataItem dataItem = e.Item as GridDataItem;
 
            ImageButton downloadButton = (ImageButton)dataItem["Download"].Controls[0];
            if (downloadButton != null)
            {
                downloadButton.Attributes.Add("OnClick", "popup");
            }
 
 
        }
}


Thanks,
Jayesh Goyani
0
Konstantinos
Top achievements
Rank 1
answered on 20 Apr 2012, 02:58 PM
Ok, thank you both, for the immediate replies.
My code works perfectly now... up until the point, where I try to call the javascript function.

Currently I have my javascript function in the Master Page, and now with the OnClick function, I try to pass the variable, through .cs rather than finding it again by .js. Thing is nothing happens.

Ok, this problem is out of the scope of this forum, I just felt the need to update.

Thank you again :)
0
Jayesh Goyani
Top achievements
Rank 2
answered on 21 Apr 2012, 08:02 AM
Hello,

Please look in below code snippet.

GridDataItem dataItem = e.Item as GridDataItem;
 
           ImageButton downloadButton = (ImageButton)dataItem["Download"].Controls[0];
           if (downloadButton != null)
           {
               downloadButton.Attributes.Add("OnClick", "window.open(\"~/DownloadPopup.aspx?filename=link\");");
           }


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Konstantinos
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jayesh Goyani
Top achievements
Rank 2
Konstantinos
Top achievements
Rank 1
Share this question
or