Clear data after submit button from text box and labels - asp.net

i have a web form that asks for the mobile number and id, if the id or mobile are incorrect. the web page will display an error label, after this error label if i entered a correct information this form will be not visible anymore and a new div will be visible with another display. so here the problem is when the customer enters the new info after a wrong info, if he clicks back the label is still appearing and text box 2, i have set these elements to:
lblfailedresponce.Text = "" // 1rsst text box
txtMobilePhone.Text = "" //2nd text box
TransactionID.Text = "" // label
but it didn't work and they still appear. so what is the code in the submit button that i have to do to clear the cache or the fields from the invisible form?

Use the following code
txtMobilePhone.Text = String.Empty;
lblfailedresponce.Text = String.Empty;

<script>
$(document).ready(function() {
// clear error label if the user clicks on the text box.
$('#txtMobilePhone').click(function(){
if('#lblfailedresponce').val()!=null)
{
$('#lblfailedresponce').val()='';
}
$('#txtMobilePhone').val()='';
$('#TransactionID').val()='';
});
$('#TransactionID').click(function(){
if('#lblfailedresponce').val()!=null)
{
$('#lblfailedresponce').val()='';
}
$('#txtMobilePhone').val()='';
$('#TransactionID').val()='';
});
});
</script>
This is a script that will serve the purpose. Kindly use this at the page bottom and see how it works.Hope this will help you.
I assume the element Id of the text boxes and its name are same. Kindly change it if its otherwise.

If it is a caching issue then you could add Response.Cache.SetCacheability(HttpCacheability.NoCache) to your page load. That will make it so the browser will not cache the page.
Another option is that you could set your error label to visible=false and then make it visible in the code that checks your error. But then the next load after that the error label will not show as visible.

Related

How can I create a name prompt before page loads and add its content to iframe src on the same page?

I have this page on my website:
https://www.lior.vip/chat
When this page loads, there is an iframe in the center of the page that runs video chat system that works like zoom.
Before the page loads, I want a popup to appear that asks the user to enter his name with two buttons: "ok / continue without name"
When user enters his name and hits "ok",
I want this name to be added to the iframe src.
For example,
if the iframe src is: "https://www.videoconf.com/room1"
I want the source to be: "https://www.videoconf.com/room1/displayName=what user entered at name prompt.
And if user hitted "continue without name",
I want the iframe src stay the regular (https://www.videoconf.com/room1).
How can I do it?
The most simple way is use prompt comand. For example, you can write this code right after body tag in your page.
<script>
let displayName = "";
const namePrompt = prompt("If you want to enter anonymous you can click 'Cancel' button, else insert your name here:");
if (namePrompt !== null) {
displayName = "/displayName="+namePrompt;
}
document.getElementById("Iframe Id").src = "your url source"+displayName;
</script>
If you prefer you can create dinamically a popup window with javascript. Or put the code inside a function and get the name as return value.

C# winforms DGV Add a button to a datagrid with variable text

I know how to add a button to a datagridview. What I am having a problem with is changing the text displayed based on whether a associated record exists in another table. I would like to change the text on the button to "View SR" or "Add SR" depending on if a service request exists for the record. if it even possible, how I would go about this ?
You just need to capture the exact cell and cast it in a Grid View Button Cell to adjust the value displayed. Following is a tried solution:
if (SomeTrueValue)
{
((DataGridViewButtonCell)dataGridView1.Rows[0].Cells[0]).Value = "Hello";
}
else
{
((DataGridViewButtonCell)dataGridView1.Rows[0].Cells[0]).Value = "World";
}

How to clear the Output message on a Submit button click using JavaScript

Hi Everyone out there..!!
I'm Facing a serious issue on clearing the retrieved message.
Flow of My project:
I'm having four text boxes and one submit button in my .aspx page.
And the four text boxes are required field.
If i give values in all the text boxes and click submit button i am getting the desired output.
This is the flow of my project.
Problem:
Now the problem is i don't have clear button in my screen.
I just clear a value from one of the text box and now I am clicking submit button.
It throws the required field validator error message as expected.
But the output from the previous execution is not getting cleared in this click event.
Expected:
I want the required field validator to throw the error and also the Output i got from the previous execution should be cleared.
This is the code i have tried
<script>
function hide()
{
document.getElementById('pnlResult').style.display = 'none';
}
</script>
I have tried this script in submit button OnClientClick() event
the problem in this is i have set required field validator for all the text but there is no value in one of the text box but it does execution and fetches me the result that
"no record is found"
Actually since one of the text box field is empty it should not do the execution at all.
You need to Put your result in particular panel and then you can hide the panel.
<script>
function hide() {
if (document.getElementById("<%=textbox1.ClientID%>").value == "" || document.getElementById("<%=textbox3.ClientID%>").value == "" || document.getElementById("<%=textbox3.ClientID%>").value == "" || document.getElementById("<%=textbox4.ClientID%>").value == "") {
var panel = document.getElementById("<%= pnlResult.ClientID%>");
if (panel) {
document.getElementById("<%=pnlResult.ClientID%>").style.display = 'none';
}
return false;
}
return true;
}
</script>
Now you need to call this script in submit Button Onclientclick

Whats the difference if I set hidden field value through html anchor OR asp.net linkbutton?

The problem is, I have a set of links onclick of those links I am setting the linkId into a Hidden field. First my link were asp:linkbutton ans onClientClick I was setting the hiddenfield value.that time I was able to get the hidden field value from code behind but when I changed my links to HTML anchor and onClick I set the hidden field value , I am not getting hidden field with blank. when I debug JavaScript it is perfectly setting the hidden field value but why I am not getting it in code behind---my code-
<a href="./ContentPage.aspx" data-flexmenu='flexmenu1' onclick="javascript:setPageLinkId(1);">
<script type="text/javascript">
function setPageLinkId(lnkPageId) {
debugger;
alert(lnkPageId);
document.getElementById('<%=hdnSelectedLink.ClientID %>').value = lnkPageId.toString();
}
</script>
//code behind- here I get blank hidden field
if (hdnSelectedLink.Value != null && hdnSelectedLink.Value != "")
{
GetLinkPage(Convert.ToInt32(hdnSelectedLink.Value));
}
Whats the problem ,Please suggest?
My theory is that the click on the anchor doesn't cause a postback to the page. Rather a HTTP GET Request to "ContentPage.aspx" is issued, meaning that any form values are not posted to the server.
You need to use a control that causes a postback to the page...for example ASP:LinkButton as you had before.
#Ozzy you were right dude.I used this in my javascript-
document.forms["aspnetForm"].submit();
its working fine now.

Using Javascript to flip flop a textbox's readonly flag

I have a frame with several radio buttons where the user is supposed to select the "Category" that his Occupation falls into and then unconditionally also specify his occupation.
If the user selects "Retired", the requirement is to prefill "Retired" in the "Specify Occupation" text box and to disable it to prevent it from being changed. The Specify Occupation text box should also no longer be a tab stop. If the user selects a radio button other than Retired the Specify Occupation text box should be enabled and once again and the Specify Occupation text box should once again be in the normal tab sequence.
Originally, I was setting and clearing the disabled property on the Specify occupation textbox, then I found out that, upon submitting the form, disabled fields are excluded from the submit and the REQUIRED validator on the Specify Occupation textbox was being raised because the textbox was being blanked out.
What is the best way to solve this? My approach below was to mimic a disabled text box by setting/resetting the readonly attribute on the text box and changing the background color to make it appear disabled. (I suppose I should be changing the forecolor instead of teh background color). Nevertheless, my code to make the textbox readonly and to reset it doesn't appear to be working.
function OccupationOnClick(sender) {
debugger;
var optOccupationRetired = document.getElementById("<%= optOccupationRetired.ClientId %>");
var txtSpecifyOccupation = document.getElementById("<%= txtSpecifyOccupation.ClientId %>");
var optOccupationOther = document.getElementById("<%= optOccupationOther.ClientId %>");
if (sender == optOccupationRetired) {
txtSpecifyOccupation.value = "Retired"
txtSpecifyOccupation.readonly = "readonly";
txtSpecifyOccupation.style.backgroundColor = "#E0E0E0";
txtSpecifyOccupation.tabIndex = -1;
}
else {
if (txtSpecifyOccupation.value == "Retired")
txtSpecifyOccupation.value = "";
txtSpecifyOccupation.style.backgroundColor = "#FFFFFF";
txtSpecifyOccupation.readonly = "";
txtSpecifyOccupation.tabIndex = 0;
}
}
Can someone provide a suggestion to me on the best way to handle this scenario and provide a tweek to the code above to fix the setting/resetting on the readonly property?
I would use jQuery instead it's super easy to implement...
To set the textbox as readonly...
$("#myTxtID").attr('readonly', 'readonly');
To set the textbox as not readonly
$("#myTxtID").removeAttr('readonly');
I recently had to do the same thing. Here's how I solved it.
go back to using the disabled property rather than readonly.
replace the RequiredFieldValidator with a CustomValidator. in your client and server validation functions, determine if you need to check the text input based on the condition of the Retired radio button. then check for input in the text box if you need to. (on the js side you can check the disabled property on the textbox itself to save yourself a step).
on the server side you should double check the radio button selection, and if "Retired" is selected, you should make sure that "Occupation" value is actually "Retired". this gets you around the issue of not getting a value from a disabled field, and you should be doing it anyway (never trust the user and all that).

Resources