如何以编程方式更改的OnClientClick事件,并调用它?
-
18-09-2019 - |
题
我有一个包含播放列表一个ListView。如果用户希望编辑的专辑,他们点击编辑链接并允许他们删除或添加歌曲到播放列表中。如果用户要保存新的播放列表,同时保持原有之一,可以点击另存为按钮。这是会发生什么:
如果用户点击另存为,他们没有改变这个名字,我想显示一个警告,告诉他们,他们必须更改名称。
如果用户点击另存为,他们已经改变了名字,我想显示一个确认,他们实际上想要保存它。
目前,正在发生的事情是,如果我把类似的东西在我的代码跟在后面,脚本不会注册,直到第二次点击和任何脚本注册保持注册的意思是,如果我最终更改名称和警报剧本是以前注册,它会显示警告,而不是确认。下面是代码:
if (newname == oldname)
{
btnSaveAs.OnClientClick =
"javascript:alert('Save As requires you to change the name of the playlist. Please
change the name and try again.');";
}
else
{
btnSaveAs.OnClientClick = "javascript:confirm('Are you sure you want to save the
playlist" + newname + "?');";
}
我也尝试添加返回false,所以它不会做回传,但如果我这样做,那么它不实际上并不当我点击确认OK做任何事情。
解决方案
什么SLaks说是正确的,你误会了页面生命周期。 JavaScript代码必须在网页上的位置的前的点击另存为。在所描述你的实施例中,用户更改的标题/名称,并点击另存为,JavaScript代码之后被施加到按钮。所述第二时间他们点击另存为,从前面的例子中的验证结果弹出。
方法1:使用验证器控制强>
解决这个最简单的方法是使用一个RegularExpressionValidator控件来比较值。
标记片段:
<asp:Label ID="lblName" runat="server" Text="Name: " />
<asp:TextBox ID="txtName" runat="server" />
<asp:RegularExpressionvalidator ID="valName" runat="server" ControlToValidate="txtName" ErrorMessage="You must change the name before saving" Display="Dynamic" />
<asp:Button ID="btnSaveAs" runat="server" OnClick="btnSaveAs_Click" Text="Save As" CausesValidation="True" />
在您的代码隐藏一旦表单字段(专辑名称等)结合,运行该:
valName.ValidationExpression = string.Format("[^{0}]", Regex.Escape(lblName.Text));
在上述正则表达式的有效期为除什么在那里开始与任何输入。如果用户更改为专辑名称的文本,保存按钮将正确验证。如果他们不这样做,验证器会踢,并显示在页面上留言说,他们必须改变它。
然后处理保存按钮的OnClick
事件仅用于保存价值,因为如果网页被验证它只会火:
protected void btnSaveAs_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
//do you save actions as needed
}
}
您也仍然可以使用您的确认框,你想,这样做:
protected void Page_Load(object sender, EventArgs e)
{
btnSave.OnClientClick = "return confirm('Are you sure you wish to change the name?');";
}
上面应该工作得很好。另一种方法是如下:
方法2:使用一个客户端验证功能强>
如果您想彻底做验证客户端,你可以但它会复杂得多。你需要做的,就是注册一个完全客户端验证功能。
在Page_PreRender期间后面的代码:
protected void Page_PreRender(object sender, EventArgs e)
{
//define the script
string script = @"
function validateAlbumName(oldName, textBoxId) {
//get the textbox and its new name
var newName = document.GetElementById(textBoxId).value;
//compare the values
if (newName === oldName) {
//if the name hasn't changed,
alert('You must change the name of the album');
return false;
}
return confirm ('Are you sure you want to save the playlist ' + newName);
}
";
//register the client script, so that it is available during the first page render
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SaveAsValidation", script);
//add the on client click event, which will validate the form fields on click the first time.
btnSaveAs.OnClickClick = string.Format("return validateAlbumName('{0}','{1}');", txtAlbumName.Text, txtAlbumName.ClientID);
}
希望这有助于。有可能一些语法错误在上面的,我只是把它迅速在一起..
其他提示
在OnClientClick
属性是Javascript字符串,而不是URI。所以,你不应该javascript:
开始吧。
要正确处理confirm
,写OnClientClick = "return confirm('Are you sure?');";
此外,你误会了ASP.Net页面模式。
您C#代码隐藏的事件处理程序只运行的之后用户点击该按钮并后的OnClientClick
回调。你需要写所有的在Javascript中并调用它在OnClientClick
。