1

嗨,我的 asp 中有一个代码可以从 mysql 数据库请求几个小时

C# 代码

        string cmd = "SELECT DepartureTime FROM dbtest";
        MySqlCommand mycmd = new MySqlCommand(cmd, conn);
        MySqlDataReader dataReader = mycmd.ExecuteReader();

        while (dataReader.Read())
        {
            times += dataReader.GetString(0) + "\n";
        }
        dataReader.Close();

Javascript

 function SendForm() {
       PageMethods.ConnectDatabase(OnSucceeded, OnFailed);
        function OnSucceeded(msg) {
            var vierkant = document.getElementById("Vierkant");
            var context = vierkant.getContext('2d');

            context.font = '15pt Calibri';
            context.fillStyle = 'blue';
            context.fillText('Departure Times bus X19:' + msg, 150, 100);
        }

当我在我的 javascript 中调用此函数时,它会将所有时间都放在一行中

我喜欢在每个结果后休息一下

我怎样才能解决这个问题?

谢谢


我尝试了以下但仍然没有工作

我的画布或变量“msg”有问题吗?

Javascript:

 var msg;
    function Connect() {
        PageMethods.ConnectDatabase(OnSucceeded, OnFailed);

        function OnSucceeded(msg)
        {

            //Set context + Formatting
            var context = document.getElementById("canvas").getContext('2d');
            context.font = '15pt Calibri';
            context.textAlign = "center";
            context.textBaseline = "top";
            context.fillStyle = 'blue';

            //Prepare textarea value to be drawn as multiline text
            var textval = msg.value;
            var textvalArr = toMultiLine(textval);
            var linespacing = 25;
            var startX = 0;
            var startY = 0;

            //Draw each line on canvas.
            for (var i = 0; i < textvalArr.length; i++) {
                context.fillText(textvalArr[i], x, y);
                y += linespacing;
            }
        }

        //Create an array where the <br/> tag splits the values.
        function toMultiLine(text) {
            var textArr = new Array();
            text = text.replace(/\n\r?/g, '<br/>');
            textArr = text.split("<br/>");
            return textArr;


            /*var vierkant = document.getElementById("canvas");
            var context = vierkant.getContext('2d');
            //formatting
            context.font = '15pt Calibri';
            context.fillStyle = 'blue';

            context.fillText('Departure Times bus X19:' + msg, 150, 100);*/
        }

        function OnFailed(error) {
            alert("failed");
        }

    }

HTML:

  <form id="Form1" runat="server">

    <input type="button" onclick="return Connect()" value="Bushours:" />

<canvas id="canvas" width="1900" height="200">Test<code>&lt;canvas&gt;</code> element.  
</canvas>
4

1 回答 1

2

如果你想休息而不是使用<br/>而不是\n

所以你的 C# 代码将是这样的:

times += dataReader.GetString(0) + "<br/>";

因为\n不是在中断中得到解释,而是<br/>

在这个答案中有一个如何做到这一点的示例: HTML5 canvas ctx.fillText won't do line breaks?

于 2013-03-13T15:16:30.373 回答