【问题标题】:Need help formatting html from my code-behind需要帮助从我的代码隐藏中格式化 html
【发布时间】:2014-04-30 20:39:06
【问题描述】:

我有以下代码 sn-p,但我正用头撞墙,试图从中找出错误。

我收到以下设计时编译错误:

; expected
The name button does not exist in the current context.

同样的两条消息也会在 DisplayReceipt 中重复。

这是我在 code behind 中为 html 分配的代码 sn-p。

有人可以帮帮我吗?

Image_ID = "<input id='" + fuelticket.Image_ID + "' type="button" onclick='" + DisplayReceipt(fuelticket.Image_ID)"'>";

【问题讨论】:

  • 我认为您需要将“”周围的按钮更改为 '' 并在末尾添加一个 + .Image_ID)。
  • 你不应该从你的代码中首先这样做。把它放在标记上,只修改后面代码中非静态的少数东西。
  • 尽量避免在c#代码中混入html。
  • 您的问题不完整,请提及 DisplayReceipt 应该作为 javascript 函数在客户端呈现。

标签: c# html


【解决方案1】:

你只需要转义引号:

Image_ID = "<input id='" + fuelticket.Image_ID + "' type=\"button\" onclick='DisplayReceipt(" + fuelticket.Image_ID + ")'>";

或者使用string.Format() 让事情变得更干净一些:

Image_ID = string.Format("<input id='{0}' type=\"button\" onclick='DisplayReceipt({0})'>", fuelticket.Image_ID);

【讨论】:

  • 越来越近...有了您提供的确切 sn-p,我现在得到“名称 DisplayReceipt 在当前上下文中不存在”,并且在“'>”的最末端;是,我得到“;预期”。
  • @sagesky36 - 已更新。在调用 DisplayReceipt() 之后,您需要一个 +。如果仍然无法解析DisplayReceipt,则需要发布更多代码。
  • 越来越接近 Jon... 我现在得到的唯一错误是“当前上下文中不存在名称 'DisplayReceipt'”。
  • 我应该提到 DisplayReceipt 是一个将被执行的 jQuery 函数。
  • 好的,那么它需要在引号内。已更新。
【解决方案2】:

要使其工作,请使用以下代码:

Image_ID = String.Format("<input id=\"{0}\" type=\"button\" onclick=\"{1}\">", fuelticket.Image_ID, DisplayReceipt(fuelticket.Image_ID));

上面看起来更清晰,您也可以选择使用@ 作为字符串,这样您就不必转义任何特殊字符。

Image_ID = String.Format(@"<input id="{0}" type="button" onclick="DisplayReceipt({0})">", fuelticket.Image_ID));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    相关资源
    最近更新 更多