如<script type="text/javascript">
//<![CDATA[
Sys.Application.initialize();
Sys.Application.add_init(function() {$create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar1_pgb"));});
Sys.Application.add_init(function() {
    $create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar2_pgb"));
});
//]]>
</script>这个是我的照别人控件做的一条进度条控件,上面是控件生成的js    <asp:ProgressBarTab ID="ProgressBar1" runat="server" CssClass="xp" Mode="Manual" Width="500" />
    <script type="text/javascript">        function pageLoad()
        {
            $find('ProgressBar1').set_percentage(62);
        }
    </script>上面这个是他们使用的方法
但是当我有多个的时候 pageLoad就只执行最后的
然后我想用ScriptManager.RegisterStartupScript(this, typeof(Page), pgb.ClientID, "$find('" + pgb.ClientID + "').set_percentage(" + Value.ToString() + ");", true);进行注册,
但结果为
<script type="text/javascript">
//<![CDATA[
$find('ProgressBar1_pgb').set_percentage(65);$find('ProgressBar2_pgb').set_percentage(15);
Sys.Application.add_init(function() {
    $create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar1_pgb"));
});
Sys.Application.add_init(function() {
    $create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar2_pgb"));
});
//]]>
</script>这里发现我的
$find('ProgressBar1_pgb').set_percentage(65);$find('ProgressBar2_pgb').set_percentage(15);Sys.Application.initialize();
在创建这个控件之前
那如何让他在他的创建之后呢
结果为//<![CDATA[
Sys.Application.initialize();
Sys.Application.add_init(function() {
    $create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar1_pgb"));
});
Sys.Application.add_init(function() {
    $create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar2_pgb"));
});
$find('ProgressBar1_pgb').set_percentage(65);$find('ProgressBar2_pgb').set_percentage(15);
//]]>
</script>
谢谢

解决方案 »

  1.   

    用 ScriptManager.RegisterStartupScript 好像无法定位脚本的输出位置,你可以试试在你要输出脚本的地方放一个Literal控件,然后在后台为它赋值:<script type="text/javascript">
    //<![CDATA[
    Sys.Application.initialize();
    Sys.Application.add_init(function() {$create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar1_pgb"));});
    Sys.Application.add_init(function() {
        $create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar2_pgb"));
    });
    //]]>
    </script>
    <asp:Literal ID="Literal1" runat="server" Text="" />Literal1.Text = "$find('" + pgb.ClientID + "').set_percentage(" + Value.ToString() + ");";
      

  2.   

    修改一下,应该要加上<script>标签:Literal1.Text = "<script>$find('" + pgb.ClientID + "').set_percentage(" + Value.ToString() + ");</script>";
      

  3.   

    楼上的这种方法不行如
    因为这个Literall在生成HTML的时候他还是在<script>前面
    如<asp:Literal ID="Literal1" runat="server" Text="哈哈,我在这里" />
    </form>以下是结果
    那是因为那一段Jscript也是由
    ScriptManager.RegisterStartupScript生成的哈哈,我在这里
    <script type="text/javascript">
    //<![CDATA[
    $find('ProgressBar1_pgb').set_percentage(65);$find('ProgressBar2_pgb').set_percentage(15);Sys.Application.initialize();
    Sys.Application.add_init(function() {
        $create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar1_pgb"));
    });
    Sys.Application.add_init(function() {
        $create(JerryWebControl.ProgressBehavior, {"Mode":0}, null, null, $get("ProgressBar2_pgb"));
    });
    //]]>
    </script>
    </form>
      

  4.   

    那试试这样呢:</form>
    <%= script %>protected string script = "";
    //某个方法里
    script = "<script type=\"text/javascript\">$find('" + pgb.ClientID + "').set_percentage(" + Value.ToString() + ");</script>";
      

  5.   

    天行健的做法比较取巧,没有说出如何控制 ScriptManager.RegisterStartupScript
    但是很实用
      

  6.   

    问题是我现在在做一个用户控件,我不太可能像上面这样给他分开来的如
    public partial class Stat_ProgressBar : System.Web.UI.UserControl
    {
        public Int32 Value
        {
            get
            {
                return ViewState["Value"] != null ? Convert.ToInt32(ViewState["Value"]) : 0;
            }
            set
            {
                ViewState["Value"] = value;
            }
        }
        public Int32 Width
        {
            get
            {
                return ViewState["Width"] != null ? Convert.ToInt32(ViewState["Width"]) : 0;
            }
            set
            {
                ViewState["Width"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            pgb.Width = Width;
            ScriptManager.RegisterStartupScript(this, typeof(Page), pgb.ClientID, "$find('" + pgb.ClientID + "').set_percentage(" + Value.ToString() + ");", true);
        }
    }
      

  7.   

    <asp:Literal ID="Literal1" runat="server" Text="" />
      

  8.   

    倒是也有办法,就是不太好:
    public partial class Stat_ProgressBar : System.Web.UI.UserControl
    {
        public Int32 Value
        {
            get
            {
                return ViewState["Value"] != null ? Convert.ToInt32(ViewState["Value"]) : 0;
            }
            set
            {
                ViewState["Value"] = value;
            }
        }
        public Int32 Width
        {
            get
            {
                return ViewState["Width"] != null ? Convert.ToInt32(ViewState["Width"]) : 0;
            }
            set
            {
                ViewState["Width"] = value;
            }
        }    public string Script{ return "$find('" + pgb.ClientID + "').set_percentage(" + Value.ToString() + ");"; }
        protected void Page_Load(object sender, EventArgs e)
        {
            pgb.Width = Width;
        }
    }
    </form>
    <%= Stat_ProgressBar1.Script %>
    <%= Stat_ProgressBar2.Script %>
    <%= Stat_ProgressBar3.Script %>
    当然也可以用我6楼的方法,先遍历一下页面上的Stat_ProgressBar控件,然后用StringBuilder把它们的Script都Append起来,然后再在前台输出