locked
How to call a java script method when a check box control is pressed, and the check box control is built in the C# code behind? RRS feed

  • Question

  • User-1410783915 posted

    I am able to create a link button and place it in a GridView control using the RowData bound event. When the hyperlink is pressed a java script method is called which shows a modal dialog box. Is it possible to do something similar with a check box control? Meaning when the check box is checked I need my java script popup dialog to be shown.
    So how can I attach a java script call to my check box button when it is pressed? I would like to add the check box dynamically to the GridView just like I am doing with the hyperlink control.

    public LinkButton lilLink = new LinkButton();
    lilLink.NavigateUrl = "javascript:window.showModalDialog('\\Summary.aspx\\?Id" + Id + "'");";

    protected void grdDisp_RowDataBound(Object sender, GridViewRowEventArgs e)
    {

    e.Row.Cells[data.Row.Table.Columns["Data"].Ordinal].Controls.Add(lilLink);

    Thursday, February 23, 2017 11:07 PM

Answers

  • User2103319870 posted

    So how can I attach a java script call to my check box button when it is pressed? I would like to add the check box dynamically to the GridView just like I am doing with the hyperlink control.

    You can use the onclick event to attach a javascript function to checkbox like below

            /// <summary>
            /// Handles the RowDataBound event of the GridView1 control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
            protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = new CheckBox();
                    chk.ID = "Checkbox"+e.Row.RowIndex;
                    DataRowView dr = (DataRowView)e.Row.DataItem;
                    chk.Attributes.Add("onclick", "jsfunction();");
    
                    e.Row.Cells[1].Controls.Add(chk);
                    e.Row.TableSection = TableRowSection.TableBody;
                }
            }

    JS Function

      <script type="text/javascript">
                    function jsfunction()
                    {
                        alert("checkbox clicked");
                    }
                </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2017 11:19 PM
  • User2103319870 posted

    IMHO if you want to add checkbox controls or any controls dynamically you should use RowCreated Event which happens at the time when a row is created in a Gridview control.

    Databound is called when you are binding data to gridview and is usually used for manipulating data

    /// <summary>
            /// Handles the RowCreated event of the GridView1 control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="GridViewRowEventArgs"/> instance containing the event data.</param>
            protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = new CheckBox();
                    chk.ID = "Checkbox" + e.Row.RowIndex;
                    DataRowView dr = (DataRowView)e.Row.DataItem;
                    chk.Attributes.Add("onclick", "jsfunction();");
    
                    e.Row.Cells[1].Controls.Add(chk);
                    e.Row.TableSection = TableRowSection.TableBody;
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2017 11:22 PM

All replies

  • User2103319870 posted

    So how can I attach a java script call to my check box button when it is pressed? I would like to add the check box dynamically to the GridView just like I am doing with the hyperlink control.

    You can use the onclick event to attach a javascript function to checkbox like below

            /// <summary>
            /// Handles the RowDataBound event of the GridView1 control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
            protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = new CheckBox();
                    chk.ID = "Checkbox"+e.Row.RowIndex;
                    DataRowView dr = (DataRowView)e.Row.DataItem;
                    chk.Attributes.Add("onclick", "jsfunction();");
    
                    e.Row.Cells[1].Controls.Add(chk);
                    e.Row.TableSection = TableRowSection.TableBody;
                }
            }

    JS Function

      <script type="text/javascript">
                    function jsfunction()
                    {
                        alert("checkbox clicked");
                    }
                </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2017 11:19 PM
  • User2103319870 posted

    IMHO if you want to add checkbox controls or any controls dynamically you should use RowCreated Event which happens at the time when a row is created in a Gridview control.

    Databound is called when you are binding data to gridview and is usually used for manipulating data

    /// <summary>
            /// Handles the RowCreated event of the GridView1 control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="GridViewRowEventArgs"/> instance containing the event data.</param>
            protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = new CheckBox();
                    chk.ID = "Checkbox" + e.Row.RowIndex;
                    DataRowView dr = (DataRowView)e.Row.DataItem;
                    chk.Attributes.Add("onclick", "jsfunction();");
    
                    e.Row.Cells[1].Controls.Add(chk);
                    e.Row.TableSection = TableRowSection.TableBody;
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2017 11:22 PM