locked
Pass this reference for checkbox event in Repeater control RRS feed

  • Question

  • User-436309264 posted

    Hello everyone,

    I am trying to pass "this" reference for checkbox click event inside Repeater Itemdatabound but getting error

     protected void repeaterAppraisalHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)

    {

               if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

               {

                           CheckBox chkGU = (CheckBox)e.Item.FindControl("chkGU");

                           chkGU.Attributes.Add("onclick", "checkGU(this" + "," + lblFiscalYear.Text + ");")

               }

    }


    function checkGU(checkbox,fiscalyear) {


    alert(fiscalyear);

    browseGU(checkBox, fiscalyear);

    }

    function browseGU(checkBox, firstCellValue) {

    $("[id *=chkGU]").each(function (e) {

    var name = $(this).prop('id');
    alert(name);

    });

    }

    I am getting Javascript error 0x800a1391 - JavaScript runtime error: 'checkBox' is undefined.     I think its not recognizing "this" reference.

    Please advice

    Thanks

    Kapil 

    Friday, February 12, 2016 3:38 AM

Answers

  • User-183374066 posted

    lblFiscalYear.Text

    From where you are getting this control. I mean you didn't find that control like you do for chkGU. By the way, you can make some changes to your above code. Put quotes around FiscalYear Text and pass Id of checkbox in function.

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    
               {
    
                           CheckBox chkGU = (CheckBox)e.Item.FindControl("chkGU");
    
                           chkGU.Attributes.Add("onclick", "checkGU(" + chkGU.ClientID +",'" + lblFiscalYear.Text + "');")
    
               }

    and in javascript

    function checkGU(checkboxId,fiscalyear) {
    
    
    alert('CheckBox checked?' + $(checkboxId).is(':checked') + ' & FiscalYear = ' + fiscalyear);
    
    
    
    }

    I didnt' test the code. You can try at your end and let me know if still you are facing issues. By the way, I prefer you to do all this event binding / javascript things on client side instead of server side. Add me on skype. I will guide you all through how to do this work at client side. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 12, 2016 4:47 AM
  • User61956409 posted

    Hi Kapils573,

    I am getting Javascript error 0x800a1391 - JavaScript runtime error: 'checkBox' is undefined.     I think its not recognizing "this" reference.

    The following sample code works fine on my side, please refer to it.

    <asp:CheckBox ID="CheckBox1" runat="server" />
    function checkGU(checkBox, firstCellValue) {
        alert($(checkBox).is(":checked") + " " + firstCellValue);
    }
    

    Code behind

    CheckBox1.Attributes.Add("onclick", "checkGU(this" + ",'2016');");

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2016 7:55 AM

All replies

  • User-183374066 posted

    lblFiscalYear.Text

    From where you are getting this control. I mean you didn't find that control like you do for chkGU. By the way, you can make some changes to your above code. Put quotes around FiscalYear Text and pass Id of checkbox in function.

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    
               {
    
                           CheckBox chkGU = (CheckBox)e.Item.FindControl("chkGU");
    
                           chkGU.Attributes.Add("onclick", "checkGU(" + chkGU.ClientID +",'" + lblFiscalYear.Text + "');")
    
               }

    and in javascript

    function checkGU(checkboxId,fiscalyear) {
    
    
    alert('CheckBox checked?' + $(checkboxId).is(':checked') + ' & FiscalYear = ' + fiscalyear);
    
    
    
    }

    I didnt' test the code. You can try at your end and let me know if still you are facing issues. By the way, I prefer you to do all this event binding / javascript things on client side instead of server side. Add me on skype. I will guide you all through how to do this work at client side. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 12, 2016 4:47 AM
  • User61956409 posted

    Hi Kapils573,

    I am getting Javascript error 0x800a1391 - JavaScript runtime error: 'checkBox' is undefined.     I think its not recognizing "this" reference.

    The following sample code works fine on my side, please refer to it.

    <asp:CheckBox ID="CheckBox1" runat="server" />
    function checkGU(checkBox, firstCellValue) {
        alert($(checkBox).is(":checked") + " " + firstCellValue);
    }
    

    Code behind

    CheckBox1.Attributes.Add("onclick", "checkGU(this" + ",'2016');");

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2016 7:55 AM