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

[Solved] RowClick within a UserControl (.ascx)

2 Answers 194 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 04 Jun 2009, 09:29 AM
Hi,

I had a radgrid in a pageview within a multipageview with some javascript to allow a windowt o open when a row in the grid is clicked. This worked fine until I changed the Multipageview so that the pageviews would load on demand (on tabclick) which I implemented by creating a user control for each pageview and moving the existing radgrid into this control.

Now when the pageview is loaded I get a javascript error on the page "Error: 'RowSelected' is undefined" and when I view the source the RowSelected function does not seem to exist. I have tried adding the javascript programatically using RegisterClientScriptBlock whithin the .ascx.cs file in the Page_Load method but this does not seem to help and I'm not sure about how to deal with the radcodeblock tags. I don't have alot of experience with javascript or user controls so it's bound to be something simple I'm missing.

Any help would be much appreciated, I can supply more code if required.

User Control:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PhoneDir.ascx.cs" Inherits="PhoneDir" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<telerik:RadCodeBlock ID="rcb1" runat="server">  
 
<script type="javascript">  
function RowSelected(sender, args)  
            {  
                var firstDataItem = $find("<%=rgPhoneDir.ClientID %>").get_masterTableView().get_dataItems()[0];  
                var uname = args.getDataKeyValue("username") ;  
                window.radopen("userinfo.aspx?id=" + uname,"RadWindow1");  
            }  
</script> 
 
</telerik:RadCodeBlock> 
<telerik:RadGrid ID="rgPhoneDir" runat="server" AllowSorting="True" GridLines="None" 
    Height="100%" Skin="Office2007" DataSourceID="SqlDataSource1"   
    onitemcommand="rgPhoneDir_ItemCommand">  
      
    <MasterTableView CellSpacing="-1" ClientDataKeyNames="username" AutoGenerateColumns="False" 
        DataKeyNames="username" DataSourceID="SqlDataSource1">  
        <Columns> 
            <telerik:GridBoundColumn DataField="username" HeaderText="username" ReadOnly="True" 
                SortExpression="username" UniqueName="username" Visible="False">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Internal Phone" HeaderText="Internal Phone" SortExpression="Internal Phone" 
                UniqueName="Internal Phone">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Surname" HeaderText="Surname" SortExpression="Surname" 
                UniqueName="Surname">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="First Name" HeaderText="First Name" SortExpression="First Name" 
                UniqueName="First Name">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Branch" HeaderText="Branch" SortExpression="Branch" 
                UniqueName="Branch">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Department" HeaderText="Department" SortExpression="Department" 
                UniqueName="Department">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Mobile" HeaderText="Mobile" SortExpression="Mobile" 
                UniqueName="Mobile">  
            </telerik:GridBoundColumn> 
        </Columns> 
        <EditFormSettings> 
            <EditColumn UniqueName="EditCommandColumn1">  
            </EditColumn> 
        </EditFormSettings> 
    </MasterTableView> 
    <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True" EnablePostBackOnRowClick="True">  
        <Selecting AllowRowSelect="True" /> 
        <ClientEvents OnRowSelected="RowSelected" OnRowClick="RowSelected" /> 
    </ClientSettings> 
</telerik:RadGrid> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:sql %>" 
    SelectCommand="SELECT username,phoneinternal AS 'Internal Phone',sname AS 'Surname',fname AS 'First Name',office AS 'Branch',department AS 'Department',mobile AS 'Mobile' FROM Phone_Dir WHERE phoneinternal IS NOT NULL AND phoneinternal &lt;&gt; '' ORDER BY office,sname">  
</asp:SqlDataSource> 
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">  
</telerik:RadWindowManager> 
<telerik:RadWindow ID="RadWindow1" runat="server">  
</telerik:RadWindow> 

When trying to add the javascript programatically:
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
        string javascript = "<script type=\"text/javascript\">";  
        javascript += "function RowSelected(sender, args){var firstDataItem = $find(\"<%=rgPhoneDir.ClientID %>\").get_masterTableView().get_dataItems()[0];var uname = args.getDataKeyValue(\"username\") ;window.radopen(\"userinfo.aspx?id=\" + uname,\"RadWindow1\");}";  
        javascript += "</script>";  
        this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page),"ClientScript", javascript);  
 
    } 

2 Answers, 1 is accepted

Sort by
0
Accepted
Iana Tsolova
Telerik team
answered on 08 Jun 2009, 10:44 AM
Hi Greg,

Please try registering the client scripts through the ScriptManager as shown below and see if this works for you:

protected void Page_Load(object sender, EventArgs e)  
{  
    string script = "function RowSelected(sender, args) { "     
        + " var firstDataItem = $find(\"" + rgPhoneDir.ClientID   
        + "\").get_masterTableView().get_dataItems()[0];   " 
        + "var uname = args.getDataKeyValue(\"username\") ;   " 
        + "window.radopen(\"userinfo.aspx?id=\" + uname,\"RadWindow1\"); } ";  
    ScriptManager.RegisterStartupScript(Page, typeof(Page), "RowSelected", script, true);  

Let me know how it goes.

Best wishes,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Greg
Top achievements
Rank 1
answered on 16 Jun 2009, 11:52 AM
Thanks Iana, worked perfectly, I knew it would be something simple!
Tags
Grid
Asked by
Greg
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Greg
Top achievements
Rank 1
Share this question
or