0
去过新的Web开发(我使用ASP.NET),我不得不通过这一目标/返回一个值,显示HTML元素,例如如输入。我完成了搜索并尝试了大部分我找到的解决方案,但都没有成功,输出仍然在HTML输入中返回一个空值。这是为什么?我的代码我的工作可以看到下面:
javacript:
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;
}
HTML:
<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" />
ASP C#:
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的一个新的Web表单:
<%@ 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>
Aspx.cs:
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);
}
}
ClientScript工作,甚至做的警告框。仍然文本框仍然是空的,不包含“whatswrong?”值
不像我试试这个:
<!DOCTYPE html>
<html>
<body>
Name: <input type="text" id="myText" value="tch">
<p>Click the button to change the value of the text field.</p>
<button onclick="confirmExistence('0')">Try it</button>
<script>
function confirmExistence(entityValue) {
alert(entityValue);
document.getElementById("myText").value = "whatswrong?";
}
</script>
</body>
</html>
在那里工作。
什么是两个,为什么它不会对ASP控件发生
我想你需要一个'onLoad'事件监听器。最后一个工作是因为你的代码位于主体的* end *,因此它在所有内容都被加载后运行。然而,一个ASP脚本管理器会把它放在'
'中,它在所有的东西都被加载之前执行。 – Santi感谢您的回复:)我试过那一个也但它不工作,即使显示它的警报对话框不起作用也不像它在标题上。将脚本放在我的contentplaceholder正文中引用母版页体的元素不起作用。 – Mheruian
这可能是由于脚本的位置,尝试将脚本移到html下面并再次运行它正在工作 – Se0ng11