Asp按钮没有添加到文本框

时间:2015-10-13 22:16:48

标签: html asp.net vb.net

我有一个多行文本框txtPostContest和几个可以点击的按钮,可以在文本框中添加HTML标记(适用于那些自己不知道任何HTML的用户)。

但是,按钮只添加一次文本,单击一个按钮后,其他按钮都不会添加文本。

HTML

<div>
    <label>Post Content:</label>
</div>
<div>
    <asp:Button ID="btnBold" runat="server" Text="Bold" Width="90px" />
    <asp:Button ID="btnItal" runat="server" Text="Italics" Width="90px" />
    <asp:Button ID="btnLink" runat="server" Text="Link" Width="90px" /> 
    <asp:Button ID="btnImage" runat="server" Text="Image" Width="90px" />
</div>
<div>
    <asp:TextBox id="txtPostContent" runat="server" Width="600px" Height="400px" TextMode="MultiLine" />
</div>

VB.Net

Partial Class blogmanager
Inherits System.Web.UI.Page

Dim bold As String = " <strong> </strong> "
Dim ital As String = " <em> </em> "
Dim img As String = " <img src=&quot;PASTE IMAGE FILE HERE&quot; alt=&quot;TYPE ALTERNATE TEXT HERE&quot; height=&quot;250&quot; width=&quot;300&quot;> "
Dim link As String = "<a href=&quot;PASTE HYPERLINK HERE&quot;>PASTE LINK TEXT HERE</a>"

 Protected Sub btnBold_Click(sender As Object, e As System.EventArgs) Handles btnBold.Click
    txtPostContent.Text += bold
End Sub

Protected Sub btnItal_Click(sender As Object, e As System.EventArgs) Handles btnItal.Click
    txtPostContent.Text += ital
End Sub

Protected Sub btnLink_Click(sender As Object, e As System.EventArgs) Handles btnLink.Click
    txtPostContent.Text += link
    txtPostContent.Text = txtPostContent.Text.Replace("&quot;", ControlChars.Quote)
End Sub

Protected Sub btnImage_Click(sender As Object, e As System.EventArgs) Handles btnImage.Click
    txtPostContent.Text += img
    txtPostContent.Text = txtPostContent.Text.Replace("&quot;", ControlChars.Quote)
End Sub

我无法在简单的text += string方法中看到问题,但显然它并不好。有没有更有效的方法将某些文本插入现有文本框?

1 个答案:

答案 0 :(得分:0)

如果您先单击任何按钮(如Bold),它将起作用并显示  

但任何其他点击都会导致错误。例如,当您单击斜体按钮

从客户端检测到潜在危险的Request.Form值(txtPostContent =&#34; &#34;)。

罪魁祸首是你在textarea中设置的文字( )。这是一个有潜在危险的文本 您可能需要谷歌搜索跨站点脚本以获取详细信息。

一旦你对所有文本调用HttpUtility.HtmlEncode函数,错误就会消失。例如     txtPostContent.Text + = HttpUtility.HtmlEncode(粗体)

您可以添加标签validateRequest =&#34; false&#34;在您的&lt;%@页面中。但这是非常不推荐!!!