如何将值从javascript传递到ASP控件

时间:2017-08-27 23:27:54

标签: javascript c# html asp.net

美好的一天,

刚开始使用Web开发(我使用ASP.NET),我的目标是传递/返回一个值以显示在HTML元素上,例如输入。我已经完成了搜索并尝试了我找到的大多数解决方案但没有工作,输出仍然在HTML输入上返回一个空值。这是为什么?我的代码我可以在下面看到:

javacript:

#include "ll.h"

LL::LL()
{
    head = NULL;
}
void LL::search (int num)//heading of the function
{
    node* newNode = new node;
        newNode->val = num;
        newNode->next = NULL;

    while (newNode == NULL)
    {
        if (newNode->val == num)
        {
            return newNode;
        }
        newNode = newNode->next; //point to the next node
    }
    return; //not sure if needed
}

HTML:

    function confirmExistence(entityValue) {
        var entity = "Staff";
        var result = "";

        if (entityValue === '0') {
            entity = "Student";
        }

        if (confirm(entity + " w/ same name is already registered. is this a different " + entity + "?")) {
            result = "Yes";
        } else {
            result = "No";
        }
        alert(result);

        document.getElementById('<%= fieldFirstNameStudent.ClientID %>').value = result;

    }

asp c#:

<asp:button class="by-button" id="btnStudentEnc" runat="server" text="Encode" OnClick="btnStudentEnc_Click" />

<asp:textbox type="text" class="mfield" placeholder="First Name" id="fieldFirstNameStudent" runat="server" />

此图片的结果如下:

enter image description here

更新:如果上述情况过于复杂。我创建了一个新的网络表单,其中包含简单的代码块,但仍无法正常工作

.aspx的:

protected void btnStudentEnc_Click(object sender, EventArgs e)
{  **some sql database condition here to run the clientscript below**
   ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
                "studentConfirmExistence", "confirmExistence('0');", true); }

Aspx.cs:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="LobbyStudents.aspx.cs" Inherits="LobbyStudents" %>

<asp:Content ID="Content0" ContentPlaceHolderID="title" Runat="Server">
    LobbyStudents
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

    <script>
        function confirmExistence(entityValue) {
            alert(entityValue);
            document.getElementById("<%= fieldFirstNameStudent %>").value = "whatswrong?";
        }
    </script>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

   <asp:textbox placeholder="First Name" id="fieldFirstNameStudent" runat="server"/>
   <asp:button runat="server" text="Encode" OnClick="btnStudentEnc_Click"></asp:button>

</asp:Content>

ClientScript可以工作,甚至可以使用警报框。仍然是文本框仍然是空的,并且不包含&#34; whatswrong?&#34;值

不像我试试这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LobbyStudents : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnStudentEnc_Click(object sender, EventArgs e)
    {
      ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
         "studentConfirmExistence", "confirmExistence('0');", true);
    }
}

它的工作原理。

两者之间的区别以及为什么它不会在asp控件上发生

1 个答案:

答案 0 :(得分:0)

好的,弄清楚你的第一个例子的问题是什么,与位置无关,但是

改变你的

document.getElementById("<%= fieldFirstNameStudent %>").value = "whatswrong?";

document.getElementById("<%= fieldFirstNameStudent.ClientID %>").value = "whatswrong?";

这将在html中生成为

document.getElementById("System.Web.UI.WebControls.TextBox").value = "whatswrong?";

其中js无法找到控件名称为 System.Web.UI.WebControls.TextBox

如果使用。ClientID进行分配,则会生成正确的ID

document.getElementById("MainContent_fieldFirstNameStudent").value = "whatswrong?";