我在页面中有一个控件,它有以下模态弹出扩展器:
<asp:Panel ID="pnl_Completed" runat="server">
<asp:Image ID="exit_Completed" runat="server" ImageUrl="" />
<h3 style="text-align:center;">Completed</h3>
<asp:Panel ID="pnl_inner" runat="server">
<table style="width:100%;height:100%" cellpadding="5px">
<tr style="height:40px;">
<td valign="top">Comment: </td>
<td>
<telerik:RadTextBox ID="txt_CompletedComment" runat="server" TextMode="MultiLine" Rows="6" Width="400" Height="100"></telerik:RadTextBox>
</td>
</tr>
<tr style="height:40px;">
<td colspan="2" align="center"><asp:Button id="btn_SaveCompleted" runat="server" Text="Complete" OnClick="btn_SaveCompleted_Click" /></td>
</tr>
</table>
</asp:Panel>
</asp:Panel>
<cc1:ModalPopupExtender ID="ModalPopupExtender_Completed" runat="server" PopupControlID="pnl_Completed" CancelControlID="exit_Completed" TargetControlID="dummy_btn_Completed" >
</cc1:ModalPopupExtender>
<asp:Button id="dummy_btn_Completed" runat="server" CssClass="display_none" />
我想在文本框为空时在btn_SaveCompleted_Click事件上显示一个消息框我试过这个:
If txt_CompletedComment.Text.Trim().Length = 0 Then
ScriptManager.RegisterStartupScript(Me, Me.GetType, "key", "alert('Please enter a comment.');", True)
End If
但这不起作用它只是隐藏模态弹出扩展器,没有错误。我做错了吗?有没有其他方式来展示它?
答案 0 :(得分:0)
只需使用JavaScript
,这里有很多方法可以做到这一点:
<asp:Button id="btn_SaveCompleted" runat="server" Text="Complete"
OnClientClick="return ValidateTheTextbox();"OnClick="btn_SaveCompleted_Click" />
<script>
function ValidateTheTextbox()
{
var txtbox = document.getElementById('<%= txt_CompletedComment.ClientID %>');
if(txtbox.value=="")
{
alert('Please enter a comment.');
return false; //suppress the submit button
}
return true; //let the form submit
}
</script>