locked
problem of javascript chain format RRS feed

  • Question

  • User-458598543 posted

    hello, i'm trying to programmed and used a javascript chain but i've got the following return error as below. Thanks for your solution.

    Détails de l'exception: System.FormatException: Le format de la chaîne d'entrée est incorrect. as the format of the entry chain is incorrected.

    protected void Click_playplayer5(object sender, EventArgs e)
    Ligne 125 :        {
    Ligne 126 :            string string_lecteur_playplayer = string.Format("function Click_playplayer5(){document.form1.getElementById(\"lecteur_audiovideo\").src = \"/audiovideo et panavideovision/Bossa nova 01.mp3\";document.form1.getElementById(\"lecteur_audiovideo\").play();"); // error
    Ligne 127 :           Page.ClientScript.RegisterStartupScript(this.GetType(), "lecteur_play_player", string_lecteur_playplayer, true);
    Ligne 128 :        }
    Tuesday, August 4, 2020 7:22 AM

Answers

  • User-1330468790 posted

    Hi Rednuts72,

     

    I would not suggest you take this way to make the audio element playing. The reason is pretty simple that the button client event won't have chance to work due to the post back. 

     

    You could refer to below codes to achieve your target.

    aspx:

     <form id="form1" runat="server">
            <div>
    
                <br />
                <audio id="lecteur_audiovideo" controls="controls" runat="server"></audio>
                <br />
    
                <br />
                <div>
                    <asp:Button ID="Button1" OnClick="Click_playplayer5" Text="play-player" runat="server" />
                    <asp:TextBox ID="TB_titre_joué_playé" runat="server"></asp:TextBox>
                    
                </div>
            </div>
        </form>

    code behind:

     protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Click_playplayer5(object sender, EventArgs e)
            {
                string string_lecteur_playplayer = "function Click_playplayer5(){var player = document.getElementById(\"lecteur_audiovideo\"); player.play();}  Click_playplayer5();";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "lecteur_play_player", string_lecteur_playplayer, true);
                lecteur_audiovideo.Attributes["src"]="/Songs/sample1.mp3";
    
            }

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 4, 2020 9:42 AM

All replies

  • User753101303 posted

    Hi,

    See https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=netcore-3.1#remarks

    {} is used for placeholders such as {0} etc... So you need to use {{ to get a single {. For now it's unclear if you really need to use String.Format.

    Tuesday, August 4, 2020 8:21 AM
  • User-458598543 posted

    i corrected the lines codes and the problem is deleted but the function doesn't worked, i would want to used a audio-video control on aspx server page but i don't succeed ; i'm trying to used javascript with an implementation on c# sharp code but it is not a succeed  ; have you got a solution ?

    <div>

    <br />
    <audio id="lecteur_audiovideo" controls="controls"></audio>
    <br />

    <br />
    <div>
    <asp:Button id="Button1" onClick="Click_playplayer5" OnClientClick="Click_playplayer5()" Text="play-player" runat="server" />
    <asp:TextBox ID="TB_titre_joué_playé" runat="server"></asp:TextBox>

    </div>
    </div>

    protected void Click_playplayer5(object sender, EventArgs e)
    {
    string string_lecteur_playplayer = string.Format("function Click_playplayer5(){{document.form1.getElementById(\"lecteur_audiovideo\").src = \"/audiovideo et panavideovision/Bossa nova 01.mp3\";document.form1.getElementById(\"lecteur_audiovideo\").play();}}");
    Page.ClientScript.RegisterStartupScript(this.GetType(), "lecteur_play_player", string_lecteur_playplayer, true);
    }

    thanks !

    Tuesday, August 4, 2020 8:32 AM
  • User-1330468790 posted

    Hi Rednuts72,

     

    I would not suggest you take this way to make the audio element playing. The reason is pretty simple that the button client event won't have chance to work due to the post back. 

     

    You could refer to below codes to achieve your target.

    aspx:

     <form id="form1" runat="server">
            <div>
    
                <br />
                <audio id="lecteur_audiovideo" controls="controls" runat="server"></audio>
                <br />
    
                <br />
                <div>
                    <asp:Button ID="Button1" OnClick="Click_playplayer5" Text="play-player" runat="server" />
                    <asp:TextBox ID="TB_titre_joué_playé" runat="server"></asp:TextBox>
                    
                </div>
            </div>
        </form>

    code behind:

     protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Click_playplayer5(object sender, EventArgs e)
            {
                string string_lecteur_playplayer = "function Click_playplayer5(){var player = document.getElementById(\"lecteur_audiovideo\"); player.play();}  Click_playplayer5();";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "lecteur_play_player", string_lecteur_playplayer, true);
                lecteur_audiovideo.Attributes["src"]="/Songs/sample1.mp3";
    
            }

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 4, 2020 9:42 AM