如何从文本框获取值以提醒

时间:2013-11-16 23:33:53

标签: c# javascript asp.net

我试图获取文本框txtID的值以显示在警报消息上。我使用此代码进行提醒:

Response.Write("<script language='JavaScript' >alert('Record successfully added!');document.location='" + ResolveClientUrl("Siteindex.aspx") + "';</script>");

它工作得很好,但是当我尝试在警报中插入txtID的值...

Response.Write("<script language='JavaScript' >alert('Record '" + txtID.Text + "' successfully added!');document.location='" + ResolveClientUrl("Siteindex.aspx") + "';</script>");

警报消息未显示,但记录已添加。什么似乎是问题?我应该使用其他警报代码吗?

3 个答案:

答案 0 :(得分:0)

转义字符串可能有些问题:

试试这个

Response.Write("<script language=\"JavaScript\" >alert(Record " + txtID.Text + " successfully added!);document.location=" + ResolveClientUrl("Siteindex.aspx") + ";</script>");

答案 1 :(得分:0)

你有一个额外的单引号,使用下面的代码

Response.Write(&#34; alert(&#39;记录&#34; + txtID.Text +&#34;成功添加!&#39;); document.location =&#39;&# 34; + ResolveClientUrl(&#34; Siteindex.aspx&#34;)+&#34;&#39;;&#34;);

答案 2 :(得分:0)

您是否使用 UpdatePanel 包装添加按钮?如果是这样,您必须为该按钮设置 PostBackTrigger

我的意思是这个示例代码

<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
        <asp:TextBox ID="txtID" runat="server" ></asp:TextBox>
        <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnAdd" />
    </Triggers>
</asp:UpdatePanel>

btnAdd 按钮由 btnAdd_Click 处理以添加记录,并运行警报脚本

Response.Write("<script language='JavaScript' >alert('Record " + txtID.Text + " successfully added!');document.location='" + ResolveClientUrl("Siteindex.aspx") + "';</script>");
相关问题