简体   繁体   English

强制复选框单击事件

[英]force checkbox click event

I have a javascript function tied to the click event of each checkbox item in an asp:checkboxlist. 我有一个JavaScript函数与asp:checkboxlist中每个复选框项的click事件相关联。 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. 我已经成功完成了此代码,添加了该项目,并将javascript函数绑定到onclick事件。 However, I need to run the onclick function once from the code behind, as I add the item to the list. 但是,当我将项目添加到列表中时,我需要从后面的代码中运行一次onclick函数。 How can I simply run a pre existing javascript function from C# codebehind? 如何简单地从C#代码隐藏中运行现有的javascript函数?

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 如果我可以强制触发刚添加的复选框的onclick事件,而不是直接调用javascript函数,那应该也可以正常工作,因为javascript函数称为复选框项目的onclick

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, ie when the dynamic checkbox is first rendered. 这样,您就可以处理onclick ,并且可以在页面回发时(即,首次显示动态复选框时)调用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM