I have a button:
<asp:Button Text="Reset" runat="server" ID="ResetButton" OnClientClick="ResetClick()"/>
that runs the following JavaScript function when it gets clicked:
function ResetClick() {
document.getElementById("<%=lblError.ClientId%>").innerHTML = "";
}
That changes the text on this label:
<asp:Label ID="lblError" runat="server" Style="color: Red;" Text=""></asp:Label>
Unfortunately, my code behind also changes the label text to an error message using the following code:
lblError.Text = "No data is available"
When I click the reset button, the text is momentarily cleared and then it fills again with the error message. Is there any way to have the code behind set a message in the label and let the JavaScript clear the message?
If you do not want the button to post back then you should add return false; to the OnClientClick attribute:
<asp:Button Text="Reset" runat="server" ID="ResetButton" OnClientClick="ResetClick(); return false;"/>
PS: adding as an answer since it sounds like it was helpful.
Related
I want to have an alert confirmation box and display the price for the user to see, however, the alert is not coming not, not sure where I went wrong. Please help
This is the method i tried.
This is the javascript
<script>
function showCFMRenewPlan() {
var renewedPlanPrice = document.getElementById("renewedPlanPrice_HD");
return confirm('Do you want to renew your plan for $' + renewedPlanPrice.innerHTML);
}
</script>
This is my button and the hiddenfield im displaying the price.
<asp:HiddenField runat="server" ID="renewedPlanPrice_HD" />
<asp:Button class="btn-primary btn-md" Font-Size="Medium" runat="server" ID="renewPlanButton" CausesValidation="false" OnClientClick="showCFMRenewPlan()" OnClick="renewPlanButton_Click1" Text="Renew Plan" />
in c# backend i added this to set the value of my hidden field
renewedPlanPrice_HD.Value = Convert.ToString(planPrice);
on my system, users have the option to "upload a doc file", "write one now" or "dont use the doc file".
I would like to validate a strng minimum using data annotations to validate the text area of "write one now", however the validation can only be triggered if "write one now" is checked.
Can this be achieved using data annotations or only with javascript?
I have one suggestion for this, use radio buttons to let user select only one, on clicking that, see if "write a row" is selected, then make the textbox or textarea as editable/ user can enter text , else let that textarea remain "Readonly". I have used this code, it is working.
<asp:RadioButton ID="rad1" runat="server" Text="upload a doc file" GroupName="vald" onchange="Check();"/>
<asp:RadioButton ID="rad2" runat="server" Text="write one now" GroupName="vald" onchange="Check();"/>
<asp:RadioButton ID="rad3" runat="server" Text="dont use the doc file" GroupName="vald" onchange="Check();"/>
<asp:TextBox TextMode="MultiLine" ID="txt" runat="server" ReadOnly="true"/>
<script type="text/javascript">
function Check()
{
if (document.getElementById("rad2").checked)
{
document.getElementById("txt").removeAttribute("readonly");
}
else
{
document.getElementById("txt").readonly = true;
}
}
</script>
I have a GridView in this GridView there is a column which I use to select date with the use of asp:CalendarExtender. I have multiple rows in GridView which are dynamically generated.
When I click the image button calender pops up and I choose my date.
In some rows I want the User to select date when he clicks image button of that row. sometime I don't want to allow him to select date.
When he tries to change the date or click on the image button, he should get alert message saying that you are not allowed to change the date.
I want to handle it from client side. So please suggest me how to do this.
this is the aspx code.
<asp:TextBox runat="server" ID="StartDateTime" CssClass="search_area_text_vm_small" onclick = "javascript:DateClick(this.id);"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/image3.jpg" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="StartDateTime" PopupButtonID="Image1" Format="yyyy-MM-dd" ></asp:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="StartDateTime"
ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
You can make use of the OnClientDateSelectionChanged event of the CalendarExtender.
Use below code:
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="StartDateTime" PopupButtonID="Image1" Format="yyyy-MM-dd" OnClientDateSelectionChanged="checkIfDateSelected()" >
</asp:CalendarExtender>
<javascript>
function checkIfDateSelected(){
var dateTextBox=document.getElementByid('StartDateTime');
if(dateTextBox!='')
{
alert ('Date once selected cannot be changed');
return false;
}
</javascript>
This will show the info pop up and prevent the form submission if the date has been selected already.
How can I make postback after user changes textbox text and hits enter ?
Is only javascript real solution. Doesnt asp.net give any out from box solution for such a problem ?
Thank You for any hints.
Beside the javascript solutions, you can place the textbox with a submit button inside a panel and make use of DefaultButton property of the panel.
Below is the code example: -
Your aspx will look like this
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>
Now, when you press enter inside the text box the button click event occurs, same as by clicking the button.
I have TextBox with RequiredFieldValidator on my page.
I also have link that calls some simple javascript.
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox1"
Display="Dynamic" />
<asp:LinkButton ID="Link1" runat="server" OnClientClick="DoSomething(); return false;" CausesValidation="false"Text="Do" />
function DoSomething() {
textbox1.val("blah"); }
When user type something into textbox and then delete that text and focus next control - then validator fires.
Then user can use link that adds text using javascript. TextBox1 text is no longer empty but RequiredFieldValidator still shows error message. How to prevent that?
you can use javascript ValidatorEnable function
ValidatorEnable(document.getElementById('<%= ValidatorID.ClientID %>'), true);
I would recommend a CustomValidator here since you do NOT want the normal RequiredFieldValidator behavior. The CustomValidator client method will only run on postback.
The whole point of the validators is that they are to be used for both server side and client side validation.
If you only want to validate on the server side, don't use a validator and simply check that the values are valid and report an error.