![](/img/trans.png)
[英]How to bind the 'SelectedValue' in dropdown in ASP.NET Web Forms C# application from code behind?
[英]How to write into chrome/ie/ff f12 console from c# asp.net application code behind
如何從 ASP.NET Web 應用程序的代碼隱藏中重現 JavaScript 的 console.log() 功能?
令人驚訝的是,這之前沒有給出,但這在 Chromes 控制台的代碼隱藏中對我有用:
Response.Write("<script>console.log('opened window');</script>")
您將無法通過背后的代碼直接寫入瀏覽器的控制台。 背后的代碼正在您的服務器上執行,瀏覽器(或消費者)不知道那里發生了什么。
如果您想從將顯示在 console.log 中的代碼隱藏傳遞消息,您必須將它們呈現到您的頁面中以便由 javascript 執行。
例如:
<script>
console.log('@(Model.your_message)');
</script>
希望這可以幫助
編輯:上面評論中的Glimpse建議加 1。
以@Joel 的回答為基礎(我還沒有評論權)......它在 Chrome 中對我有用,我構建了這個簡單的擴展方法,使其更容易並適用於我的所有代碼隱藏頁面:
public static void ConsoleLog(this Page page, string msg)
{
msg = msg.Replace("'", ""); //Prevent js parsing errors. Could also add in an escape character instead if you really want the 's.
page.Response.Write("<script>console.log('"+msg+"');</script>");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.