Radgrid AddNewRecordButton OnClientClick event

3 posts, 0 answers
  1. Bijal
    Bijal avatar
    3 posts
    Member since:
    Aug 2013

    Posted 29 Oct 2013 Link to this post

    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. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 30 Oct 2013 Link to this post

    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.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Bijal
    Bijal avatar
    3 posts
    Member since:
    Aug 2013

    Posted 30 Oct 2013 Link to this post

    This works well. Thank you!
Back to Top