当我使用 ScriptManager.RegisterStartupScript 时,RequiredFieldValidator 无法正常工作
RequiredFieldValidator not correct working when i use ScriptManager.RegisterStartupScript
当我触发 btn 事件时。刷新页面和 RegisterStartupScript 后,我的 asp.net 验证器无法正常工作
protected void btnSumbit_Click(object sender, EventArgs e)
{
NotifyShow("example");
}
public void NotifyShow(string msg)
{
string myScript = string.Format("ShowMessages('{0}','{1}');", msg, "type");
StringBuilder str = new StringBuilder();
str.Append(myScript);
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
"AlertJS", str.ToString(), true);
}
这个HTML
<form id="form1" runat="server">
<div id="main">
<asp:TextBox runat="server" ID="txtb1" ValidationGroup="g1" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtb1" Text="not valid" ValidationGroup="g1"></asp:RequiredFieldValidator>
<asp:Button runat="server" ID="btnSumbit" ValidationGroup="g1" OnClick="btnSumbit_Click"/>
</div>
</form>
这个javascript
function ShowMessages(messageText, type) {
var images = "";
document.getElementById("main").innerHTML = document.getElementById("main").innerHTML + "<div id='MessagePopUp' class='" + type + "' title='Error message'>" +"" +"<p id='MessageText' style='direction: rtl; margin: 0px auto; padding: 5px;'>" + messageText + "</p>" +"</div>";
var popUp = document.getElementById("MessagePopUp");
$("#MessagePopUp").show("slide", 1000, function () {
$("#MessagePopUp").hide("slide", 2000, function () {
document.getElementById("main").removeChild(popUp);
});
}).delay(4000);}
我在将我的 js 代码更改为 :
时解决了这个问题
function ShowMessages(messageText, type) {
var html='<div id="MessagePopUp" class="' + type + '" title="Error message"><p id="MessageText" style="direction: rtl; margin: 0px auto; padding: 5px;">' + messageText + '</p></div>';
addElement('main', 'div', 'tempElement', html);
var popUp = $("#MessagePopUp");
$("#MessagePopUp").show("slide", 1000, function () {
$("#MessagePopUp").hide("slide", 2000, function () {
removeElement('tempElement');
});
}).delay(4000);
}
function addElement(parentId, elementTag, elementId, html) {
// Adds an element to the document
var p = document.getElementById(parentId);
var newElement = document.createElement(elementTag);
newElement.setAttribute('id', elementId);
newElement.innerHTML = html;
p.appendChild(newElement);
}
function removeElement(elementId) {
// Removes an element from the document
var element = document.getElementById(elementId);
element.parentNode.removeChild(element);
}
当我触发 btn 事件时。刷新页面和 RegisterStartupScript 后,我的 asp.net 验证器无法正常工作
protected void btnSumbit_Click(object sender, EventArgs e)
{
NotifyShow("example");
}
public void NotifyShow(string msg)
{
string myScript = string.Format("ShowMessages('{0}','{1}');", msg, "type");
StringBuilder str = new StringBuilder();
str.Append(myScript);
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
"AlertJS", str.ToString(), true);
}
这个HTML
<form id="form1" runat="server">
<div id="main">
<asp:TextBox runat="server" ID="txtb1" ValidationGroup="g1" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtb1" Text="not valid" ValidationGroup="g1"></asp:RequiredFieldValidator>
<asp:Button runat="server" ID="btnSumbit" ValidationGroup="g1" OnClick="btnSumbit_Click"/>
</div>
</form>
这个javascript
function ShowMessages(messageText, type) {
var images = "";
document.getElementById("main").innerHTML = document.getElementById("main").innerHTML + "<div id='MessagePopUp' class='" + type + "' title='Error message'>" +"" +"<p id='MessageText' style='direction: rtl; margin: 0px auto; padding: 5px;'>" + messageText + "</p>" +"</div>";
var popUp = document.getElementById("MessagePopUp");
$("#MessagePopUp").show("slide", 1000, function () {
$("#MessagePopUp").hide("slide", 2000, function () {
document.getElementById("main").removeChild(popUp);
});
}).delay(4000);}
我在将我的 js 代码更改为 :
时解决了这个问题function ShowMessages(messageText, type) {
var html='<div id="MessagePopUp" class="' + type + '" title="Error message"><p id="MessageText" style="direction: rtl; margin: 0px auto; padding: 5px;">' + messageText + '</p></div>';
addElement('main', 'div', 'tempElement', html);
var popUp = $("#MessagePopUp");
$("#MessagePopUp").show("slide", 1000, function () {
$("#MessagePopUp").hide("slide", 2000, function () {
removeElement('tempElement');
});
}).delay(4000);
}
function addElement(parentId, elementTag, elementId, html) {
// Adds an element to the document
var p = document.getElementById(parentId);
var newElement = document.createElement(elementTag);
newElement.setAttribute('id', elementId);
newElement.innerHTML = html;
p.appendChild(newElement);
}
function removeElement(elementId) {
// Removes an element from the document
var element = document.getElementById(elementId);
element.parentNode.removeChild(element);
}