Add javascript to the client

Show how to add script to the client in aspx file.

/// <param name="rbl">RadioButtonList to apply script to</param>
/// <param name="page">The Page the script is going to be appended to</param>
/// <param name="script">The script to append</param>
public static void SetRadioButtonListItemScript(RadioButtonList rbl, Page page, string script)
{
    for (int idx = 0; idx < rbl.Items.Count; idx++)
    {
        RegisterClientObjectFunction(page, rbl, idx, script);
    }
}

/// <param name="page">The Page the script is going to be appended to</param>
/// <param name="rbl">RadioButtonList to apply script to</param>
/// <param name="idx">the index of the radio button</param>
/// <param name="script">The script to append</param>
static private void RegisterClientObjectFunction(Page page, RadioButtonList rbl, int idx, string script)
{
    StringBuilder sw = new StringBuilder();
    if (!page.IsStartupScriptRegistered(rbl.ClientID + "_" + idx.ToString() + "script"))
    {
        sw.Append(@"<SCRIPT>");
        sw.Append(@"document.getElementById('" + rbl.ClientID + "_" + idx.ToString() + "').onclick=function() {" + script + "return true;}");
        sw.Append(@"</SCRIPT>");
        page.RegisterStartupScript(rbl.ClientID + "_" + idx.ToString() + "script", sw.ToString());
    }
}

static private void RegisterClientObjectFunction(Page page, CheckBox chk, string script)
{
    StringBuilder sw = new StringBuilder();
    if (!page.IsStartupScriptRegistered(chk + "script"))
    {
        sw.Append(@"<SCRIPT>");
        sw.Append(@"document.getElementById('"+chk.ClientID + "').onclick=function() {" + script + "return true;}");
        sw.Append(@"</SCRIPT>");
        page.RegisterStartupScript(chk.ClientID + "script", sw.ToString());
    }
}

posted on 2007-08-30 18:34  Wade  阅读(411)  评论(0编辑  收藏  举报

导航