I've to send mail using webappplication.All works fine but i've to show a confirm message if attachments are not selected.In that confirm box when ok is clicked execution of the code should continue,when cancel button is clicked it should return.
This is my code
Code behind
protected void btnSend_Click(object sender, EventArgs e)
{
string str = "Are you sure, you want to proceed without attachment?";
this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true);
...Send mail code goes here
}
ASPX
function ConfirmApproval(objMsg)
{
if(confirm(objMsg))
{
alert("execute code.");
return true;
}
else
return false;
}
It works fine, ie when ok button is clicked an alert "execute code" is displaying.Instead of displaying alert i want to continue execution of the code.How will we do that from client side ???
try this code,
protected void Page_Load(object sender, EventArgs e)
{
btnSend.Attributes.Add("onclick","return confirm('execute code');");
}
You need to use confirm("execute code") instead of 'alert'. Hope that helps :)
var agreed = confirm("execute code");
if(agreed) {
//do something
} else {
return false;
}
I am showing you one brute force way to do that.
put that code behind in the page_load(sender, e) of any web page you create. Now call it through XHR.
(this may be a bad reply).
Related
Clarification: I am chilean, so my english is not perfect, sorry for the misspellings.
Hi, I am working with an image in c#.
I try to put an example image when the page open the first time, I used the post back for this, but when I press a button, execute the code in the post back section (which is right), after that it execute the Button code, but then, it pass again for the Page_Load method, and execute the "not post back" section, and i dont know why.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//Is post back
}
else // Is not post back
{
//Make things only when the page is open for the first time
}
}
I usually only use (!IsPostBack) on PageLoad for doing initial data loads or validations(like settings for users).
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (userIsAdmin)
{
button1.Enabled = true;
}
}
}
You could refer to the link for the explanation for PostBack https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8
Im working with errorprovider in a c# winforms application.
Now I want to have a "double" validation. Once on the textfields directly, so the user sees that he has made some errors, and once on the button itself. So when there are still errors, the "save" button will keep greyed out or "disabled".
Because I don't want to block my user when he is making an error, and I want him to be able to make the changes whenever he wants im using the event "leave" or on focus lost. This because otherwise I noticed you cannot go to another field, until you changed your error.
So, now the code:
private void txtFirstname_Leave(object sender, EventArgs e)
{
if (!InputChecks.IsFilledIn(txtFirstname.Text))
{
errorProvider1.SetError(txtFirstname, "Firstname needs to be filled in!");
isValidated = false;
}
else
{
errorProvider1.SetError(txtFirstname, "");
isValidated = true;
}
}
So far, so good. The error provider works correctly and my user can edit whenever he wants.
public void setSaveButton()
{
if (isValidated == true)
{
btnSave.Enabled = true;
}
else
{
btnSave.Enabled = false;
}
}
bool isValidated;
private void btnSave_Click(object sender, EventArgs e)
{
if (isValidated == true)
{
employeePresenter.addEmployee(txtFirstname.Text, txtLastname.Text, txtUsername.Text, txtPassword.Text);
}
}
This was still okey in my head. BUT, as I give the ability to the user to change the issues whenever they want, this doesn't work. I tried to put the method "setSaveButton()" under "isvalidated" but this is not working either. Because of the focus lost.
Anyone has a better idea for this? I have been looking on google and the only things i found was a single validation with the errorprovider, or the event validating. But these events don't allow users to edit their errors whenever they want. It blocks them into one particular text field.
You don't need to make the save button disabled. It's enough to check ValidateChildren method of your form and if it returned false, it means there is some validation error. To use this approach you should remember to set e.Cancel = true in Validating event of the control when you set an error for control.
Also to let the user to move between controls even if there is an error, set AutoValidate property of your Form to EnableAllowFocusChange in designer or using code:
this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;
Code for Validation:
private void txtFirstname_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrEmpty(this.txtFirstname.Text))
{
this.errorProvider1.SetError(this.txtFirstname, "Some Error");
e.Cancel = true;
}
else
{
this.errorProvider1.SetError(this.txtFirstname, null);
}
}
private void btnSave_Click(object sender, EventArgs e)
{
if (this.ValidateChildren())
{
//Here the form is in a valid state
//Do what you need when the form is valid
}
else
{
//Show error summary
}
}
I'm quite new to C# and I'm working on a form that logs into a website, navigates to a specific page and then it should check if that page contains the words "Registered Plus" (This all in a webBrowser) Now I got this all working, except for the last part. I have been thinking and searching for hours about how to make my application check if the current webpage contains "Registered Plus"... This is my code for the button so far:
private void btnReboot_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username")[0].SetAttribute("value", usernameBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", passwordBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("submit")[0].InvokeMember("click");
webBrowser1.Navigate("http://website.com/login.php?action=login");
}
Does anyone know how to make it check if this page: http://website.com/login.php?action=login contains "Registered Plus" ? Or maybe a tutorial about how to do something similar to this? Thanks alot in advance. Have been stuck on this part for quite a while now..
UPDATE:
Got a comment telling me about DocumentText.Contains, tried this:
private void btnReboot_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username")[0].SetAttribute("value", usernameBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", passwordBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("submit")[0].InvokeMember("click");
webBrowser1.Navigate("http://darkbox.nl/usercp.php?action=usergroups");
if (webBrowser1.DocumentText.Contains("Registered Plus"))
{
label3.Text = "You're plus";
}
else
{
label3.Text = "You're not plus";
}
}
However it still tells me "You're not plus"
Am I doing it right this way? Or..
I am not able to test the code right now, but the big issue is that the call to the webBrowser1.Navigate is executed asynchroniously. Just like when you request it in IE or Chrome, it takes anywhere from a second to a minute for the page to load (or give an error.) On the other hand, your C# code takes barely a millisecond to move from the Navigate request to the next line of code.
You need to fire off your code checking the Document once the Navigate() method returns an event indicating it is done.
private bool shouldEvaluateReponse = false;
private void btnReboot_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username")[0].SetAttribute("value", usernameBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", passwordBox.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("submit")[0].InvokeMember("click");
shouldEvaluateResponse = true;
webBrowser1.Navigate("http://darkbox.nl/usercp.php?action=usergroups");
}
public void WebBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
//ignore this method if the flag isn't set.
if (!shouldEvaluateResponse) return;
//reset the flag so this method doesn't keep executing
shouldEvaluateResponse = false;
if (webBrowser1.DocumentText.Contains("Registered Plus"))
{
label3.Text = "You're plus";
}
else
{
label3.Text = "You're not plus";
}
}
Here's what I want to do. I have a form that the user fills out (creating sections and subsections) and when they click save, I want to check the database to see if they have named a section the same as one that already exists. If they have, I want to get a confirmation from them to let them know they wil create a duplicate if they proceed. If they click yes, I need to continue, else I need to abort. Here is some psuedocode for what I have so far.
protected void SaveButton_Click(object sender, EventArgs e)
{
try
{
if (CheckForDuplicates())
{
//proceed normally
}
}
}
private bool CheckForDuplicates()
{
//check database
if (/*there are duplicates*/)
{
string message = "A duplicate name exists. Would you like to continue?";
string scriptString = "<script language='javascript'
type='text/javascript'>" + "return confirm('" + message + "');</script>";
ScriptManager.RegisterStartupScript(this, this.GetType(),
"script", scriptString, false);
//here i would like to return their confirmation
}
}
}
return true;
}
All help is appreciated and thanks in advance!
Add Javascript such that if user confirms, you can call the JavaScript __doPostBack('','UserConfirmed'); function. Just add the logic in your codebehind along with your confirmation logic that you are registering with the ScriptManager . When the postback occurs, you can then check to ensure that the postback was, in fact, initiated by the user's confirmation (as opposed to some other action on the page):
public void Page_Load(object sender, EventArgs e)
{
string parameter = Request["__EVENTARGUMENT"];
//if parameter equals "UserConfirmed"
// User confirmed, so do whatever
//
}
Information on __doPostBack: Understanding the JavaScript __doPostBack Function
I have a button on my aspx page. I want to use javascript confirm before continuing execution when clicking on that button. I can do it easily if i am writing javascript in aspx page itself . But my problem is each time the confirm message may be different. I need to check various condition to generate appropriate confirm message.
Can I call confirm in my code behind, so that I can construct confirm message from there?
What I'm trying is:
protected void Button1_Click(object sender, EventArgs e)
{
//just the algorithm given here
string message=constructMessage(); \\ its just a function to construct the confirm message
if(confirm(message)) // i know i cant use javascript in code behind direct. How can i do this
{
//do something
}
else
{
// do nothing
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string message=
"if(confirm("+message+"))
{
//do something
}
else
{
// do nothing
}";
this.ClientScriptManager.RegisterStartupScript(typeof(this.Page), "warning", message, true);
//Prints out your client script with <script> tags
}
For further reference on ClientScriptManager
I just got this link which describes different ways of calling javascript
http://www.codedigest.com/Articles/ASPNET/314_Multiple_Ways_to_Call_Javascript_Function_from_CodeBehind_in_ASPNet.aspx
may be this will help..