1

I have a javascript function tied to the click event of each checkbox item in an asp:checkboxlist. However, there's an option whereby the user can dynamically add a new item to the list. I have already successfully done this code, the item is added, and the javascript function tied to the onclick event. However, I need to run the onclick function once from the code behind, as I add the item to the list. How can I simply run a pre existing javascript function from C# codebehind?

protected void btnAddLang_Click(object sender, EventArgs e)
{
        Healthline.ServiceRecords.DataHelper helper = new Healthline.ServiceRecords.DataHelper();
        Hashtable prms = new Hashtable();
        DataSet dsLangs = new DataSet();

        prms = new Hashtable();
        prms.Add("@LangName", txtOtherLanguages.Text);

        dsLangs = helper.RunSP("mySP", prms);

        if (dsLangs.Tables[0].Rows.Count > 0)
        {
            ListItem item = new ListItem();
            item.Value = dsLangs.Tables[0].Rows[0]["LanguageID"].ToString();
            item.Text = dsLangs.Tables[0].Rows[0]["Language"].ToString();
            chkTopLanguages.Items.Add(item);
        }

        foreach (ListItem listitem in chkTopLanguages.Items)
        {
            listitem.Attributes.Add("onclick", "GetLanguages()");
        }

        //here I need to force an immediate call to the javascript function GetLanguages()
    }

If it helps. Rather than a straight call to the javascript function, if I can force a trigger to the onclick event of the checkbox I just added, that should work as well, since the javascript function is called onclick of the checkbox item

Mark Highfield
  • 443
  • 3
  • 9
  • 24

1 Answers1

0

I would suggest adding an argument to the function, and passing in the control. That way you can handle the onclick, and you can call the function when the page is posted back, i.e. when the dynamic checkbox is first rendered.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • That's not an answer, but a comment. The OP is asking how to "fire" off an onclick() event from code behind, which is what I am after. – Fandango68 Mar 30 '17 at 02:13