将提示框中输入的值写入asp.net中的文本框

时间:2012-04-02 12:08:37

标签: javascript asp.net prompt

enter image description here我有以下代码,我希望ser在提示框中输入的值到文本框,这是一个asp.net控件我该怎么做?请帮忙。

 protected void btnPreview_Click(object sender, EventArgs e)
    {
        divTemplateDesigner.Visible = true;
        divQueryBuilder.Visible = false;
        int count = 0;
        string[] splitMessage = txtTemplate.Text.Split(' ');
        List<string> parameterList = new List<string>();
        List<string> valueList = new List<string>();
        string newMessageString = txtTemplate.Text;
        for (int i = 0; i < splitMessage.Length; i++)
        {
            if (splitMessage[i].StartsWith("["))
            {
                parameterList.Add(splitMessage[i]);
                count++;
            }
       }
        for (int j = 0; j < count; j++)
        {
            string message ="Enter "+parameterList[j];
            Label lblmsge = new Label();
            lblmsge.Text =
                "<script language='javascript'>" + Environment.NewLine + "window.prompt('" + message + "')</script>";
            Page.Controls.Add(lblmsge);
        }
    }

提示框正确显示。但是我无法捕获用户输入的值。

HTML就像这样

 <div class="page" runat="server" id="divTemplateDesigner">
    <table bgcolor="#0CB1C0" style="width:100%;">
        <tr>
            <td bgcolor="White" class="style5" rowspan="2">
                <asp:TreeView ID="treeviewDataset" runat="server" 
                    onselectednodechanged="treeviewDataset_SelectedNodeChanged">
                </asp:TreeView>
            </td>
            <td>
                <asp:TextBox ID="txtTemplate" runat="server" Height="200px" 
                    TextMode="MultiLine" Width="370px"></asp:TextBox>
            </td>
            <td colspan="2">
                <br />
                <br />
                <asp:Button ID="btnPreview" runat="server" BackColor="White" Height="30px" 
                    Text="Preview" Width="120px" onclick="btnPreview_Click" />
            </td>
            <td>
                <asp:TextBox ID="txtPreview" runat="server" Height="200px" TextMode="MultiLine" 
                    Width="370px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left" colspan="2">
                &nbsp;
                <asp:Button ID="btnBack" runat="server" BackColor="White" Height="30px" 
                    Text="Back" Width="120px" onclick="btnBack_Click" />

                                                                                 

您在此图片中看到该框有三个占位符[patient_ID] [Doctor_ID]和[Appointment_Date] 打开提示框取决于第一个框中的占位符数。 Count =占位符数

1 个答案:

答案 0 :(得分:2)

只需使用Javascript来完成您的目标,而不是回发到服务器。

<script type="text/javascript">
    function PromptAndFillPreview() {
        var templateStr = document.getElementById('<%= txtTemplate.ClientID %>').innerText;
        var previewStr = '';
        var phIndexStart = templateStr.indexOf('[');
        var phIndexEnd = 0;
        while (phIndexStart >= 0) {
            previewStr = previewStr + templateStr.substring(phIndexEnd, phIndexStart);
            phIndexEnd = templateStr.indexOf(']', phIndexStart) + 1;
            var placeholder = templateStr.substring(phIndexStart, phIndexEnd);
            previewStr = previewStr + prompt("Please input value for " + placeholder, '');
            phIndexStart = templateStr.indexOf('[', phIndexEnd);
        }
        previewStr = previewStr + templateStr.substring(phIndexEnd, templateStr.length);
        document.getElementById('<%= txtPreview.ClientID %>').innerText = previewStr;
    }
</script>

由于您不需要回发,因此可以替换:

<asp:Button ID="btnPreview" runat="server" BackColor="White" Height="30px" 
    Text="Preview" Width="120px" onclick="btnPreview_Click" />

使用:

<input type="button" style="background-color:White; height:30px; width:120px;"
    value="Preview" onclick="PromptAndFillPreview()" />