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

Radgrid AddNewRecordButton OnClientClick event

2 Answers 230 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Bijal
Top achievements
Rank 1
Bijal asked on 30 Oct 2013, 12:21 AM

I'm trying to add some javascript that will run when the user clicks on the "Add new record" button.

I can do this with a regular linkbutton like this:

<asp:LinkButton ID="LinkButton5" runat="server" CssClass="gridLink" CommandName="PerformInsert"
Text="Insert" OnClientClick="javascript:hideConfirm = true;" />

How would I do this for the Add new record button? I've attached the code below. Or maybe I could add my javascript from the code behind, but how would I find that control? I've added an image of the "Add" button. Thanks in advance for your help.

<telerik:RadGrid ID="rgComments" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
 
    AllowAutomaticUpdates="True" AllowSorting="True" CellSpacing="0"
 
    GridLines="None" Width="800px" AutoGenerateColumns="False" BorderWidth="0px">
 
    <MasterTableView CommandItemDisplay="Top" DataKeyNames="idPartnerCommentId" EditMode="InPlace">
 
        <CommandItemSettings ExportToPdfText="Export to PDF" ShowRefreshButton="False" AddNewRecordText="Add Comment" />
 
        <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
 
            <HeaderStyle Width="20px"></HeaderStyle>
 
        </RowIndicatorColumn>
 
        <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
 
            <HeaderStyle Width="20px"></HeaderStyle>
 
        </ExpandCollapseColumn>
 
<telerik:RadGrid ID="rgComments" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
 
    AllowAutomaticUpdates="True" AllowSorting="True" CellSpacing="0"
 
    GridLines="None" Width="800px" AutoGenerateColumns="False" BorderWidth="0px">
 
    <MasterTableView CommandItemDisplay="Top" DataKeyNames="idPartnerCommentId" EditMode="InPlace">
 
        <CommandItemSettings ExportToPdfText="Export to PDF" ShowRefreshButton="False"
 
AddNewRecordText="Add Comment" />
 
        <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
 
            <HeaderStyle Width="20px"></HeaderStyle>
 
        </RowIndicatorColumn>
 
        <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
 
            <HeaderStyle Width="20px"></HeaderStyle>
 
        </ExpandCollapseColumn>
 
        <Columns>
 
            <telerik:GridTemplateColumn DataField="varcharComment"
 
                HeaderText="Comment" SortExpression="varcharComment" UniqueName="varcharComment">
 
                <EditItemTemplate>
 
                    <asp:TextBox ID="txtComment" TextMode="MultiLine" Width="430px" Height="60px"
 
runat="server" MaxLength="5000" />
 
                </EditItemTemplate>
 
                <ItemTemplate>
 
                    <asp:Label ID="lblComment" runat="server"></asp:Label>
 
                </ItemTemplate>
 
            </telerik:GridTemplateColumn>
 
        </Columns>
 
    </MasterTableView>
 
</telerik:RadGrid>
 
        <Columns>
 
            <telerik:GridTemplateColumn DataField="varcharComment"
 
                HeaderText="Comment" SortExpression="varcharComment" UniqueName="varcharComment">
 
                <EditItemTemplate>
 
                    <asp:TextBox ID="txtComment" TextMode="MultiLine" Width="430px" Height="60px" runat="server" MaxLength="5000" />
 
                </EditItemTemplate>                <ItemTemplate>
 
                    <asp:Label ID="lblComment" runat="server"></asp:Label>
 
                </ItemTemplate>
 
            </telerik:GridTemplateColumn>
 
        </Columns>
 
    </MasterTableView>
 
</telerik:RadGrid>


2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Oct 2013, 08:43 AM
Hi Bijal,

Please have a look into the code snippet I tried to call a JavaScript function by clicking on "Add New Record" Button of RadGrid.

C#:
protected void rgComments_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridCommandItem)
    {
        Button btn = (Button)e.Item.FindControl("AddNewRecordButton");
        btn.Attributes.Add("onClick", "demo()");
        LinkButton lbtn = (LinkButton)e.Item.FindControl("InitInsertButton");
        lbtn.Attributes.Add("onClick", "demo()");
    }
}

JavaScript:
<script type="text/javascript">
    function demo() {
        alert("JavaScript Called");
    
</script>

Multiple controls with the same ID 'txtComment' were found, it is always advisable to use different ID for the Controls inside a RadGrid.

Thanks,
Shinu.

0
Bijal
Top achievements
Rank 1
answered on 30 Oct 2013, 04:33 PM
This works well. Thank you!
Tags
Ajax
Asked by
Bijal
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Bijal
Top achievements
Rank 1
Share this question
or