Am using the below message box in asp.net web application. Now i want to convert this message box as a confirmation message box and do something when it is true else means reject the application.
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Are you sure, you want to apply?');</script>", false);
I think you are going about this the wrong way. You should display this confirmation before posting back, and then only post back if they choose to "Apply".
Using ASP.NET web controls, the Button control has an OnClientClick property which can be used to call javascript prior to Http POST:
You could do something like this:
<asp:button id="btn"
runat="server"
Text="Apply"
OnClientClick="return confirm('Are you sure you wish to apply?');"
OnClick="btn_Click" />
register script bellow instead of alert
<script type="text/javascript">
var r=confirm("Are you sure you are sure?")
if (r==true)
{
//You pressed OK!
}
else
{
//You pressed Cancel!
}
</script>
The javascript equivalent to a confirmation box is the confirm() method. This method returns a true or false value depending on the user's "OK" or "Cancel" button response.
Usage:
var confirmed = confirm('Are you sure?','Are you sure you want to delete this item?');
if(confirmed){
//do something
} else {
//do something else
}
Try this
Add this on your cs file to display a confirm instead of alert
string confirm =
"if(confirm('Are you surely want to do this ??')) __doPostBack('', 'confirmed');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", confirm, true);
Add this on same page to check when user is coming from that confirmation box
protected void Page_Load(object sender, EventArgs e)
{
string parameter = Request["__EVENTARGUMENT"];
if (string.Equals("confirmed",
parameter,
StringComparison.InvariantCultureIgnoreCase))
{
// Call your server side method here
}
}
For this I used __doPostBack you can learn more about it from here.
Hope it'll help you
private void showMessage(string msg){
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('"+ msg +"');</script>", false);
protected void BtnReg_Click(object sender, EventArgs e)
{
OracleHelper.OracleDBOpen();
object flag = OracleHelper.OracleExecuteScalar("your select Query ");
if (flag == null)
{
showMessage("Failed !!! ");
}
else
{
string reg = String.Format("your Insert Query ");
showMessage("successfuly");
OracleHelper.OracleExecuteNonQuery(reg);
}
OracleHelper.OracleDBClose();
}
}
To do this completely within C#, you can try this:
protected override void OnInit(EventArgs e)
{
AddConfirmationButton();
base.OnInit(e);
}
private void AddConfirmationButton()
{
Button confirmButton = new Button();
confirmButton.Text = "Action Foo";
string confirmationMessage = "Are you sure you wish to do action Foo?";
confirmButton.OnClientClick = "return confirm('" + confirmationMessage + "');";
confirmButton.Command += confirmButton_Command;
Controls.Add(confirmButton);
}
void confirmationMessage_Command(object sender, CommandEventArgs e)
{
DoActionFoo(); //work your magic here.
}
This presents and "OK/Cancel" dialog box to the user from the webpage. If the user clicks 'ok', the function from the command Event fires. If the user clicks 'cancel', nothing happens.
ScriptManager.RegisterStartupScript(page,this.GetType(), "temp","javascript:calopen();
",true);
function calopen()
{
if (confirm("Are you sure?"+'\n'+"Are you want to delete"))
{
enter code here
}
else
{
return false;
}
}
try this code check on OnClientClick event when user click on button
<script type="text/javascript">
function check()
{
var chk =confirm("Are you sure you are sure?")
if (chk==true)
{
// try you want
}
else
{
// try you do not want
}
return true;
}
</script>
<asp:button id="Button1"
runat="server"
Text="Button1"
OnClientClick="return check();"/>
Related
i understand that we able to achieve this by using "onClientClick", but i want to check the validation first BEFORE the confirmation box.
javascript
function showConfirm() {
var result = window.confirm('Are you sure?');
if (result == true)
alert("ok");
}
html
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
C#
protected void Button1_Click(object sender, EventArgs e)
{
if(checkValidation() == true)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "javascript:showConfirm();", true);
//if(result == true) //how to get the result value?
//{
////run some code
//insert data into sql
//}
}
}
is there anyway i can get the confirmation result at code behind? without the if-else-statement, the data will insert into sql before user choose their decision.
i create a button with display:none
.hideButton{
display:none;
}
create code behind
protected void btnConfirm_Click(object sender, EventArgs e)
{
Response.Redirect("page2.aspx");
}
trigger the btnConfirm click if user click Yes on confirmation box
$('#ContentPlaceHolder1_btnConfirm').trigger('click');
If you are doing a custom validation in Web forms that you much enable EnableClientScript="true" and write the JS validate function name in ClientValidationFunction="JSValidateFunctionName" like this you would be able to validate on client side before even going to server side.
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
and my code behind code is
protected void btnSave_Click(object sender, EventArgs e)
{
string confirmValue = null;
confirmValue = Request.Form["confirm_value"];
if (confirmValue == "No")
{
some code....
}
else
{
return;
}
}
What actually gonig is confirmValue storing the values like yes,No,Yes,No.
But for first time it will show confirmation message, I will click on Ok button and some entries will done then I click on save button it will show again message box at this time I will click on Cancel button then it will go into the if condition but confirmvalue is storing First clicked yes and Next time clicked no so it is going into else condition ....
what i have to do please help me b
The cleanest way is to simply use the "OnClientClick" property of the button control like so:
OnClientClick="return confirm('Do you want to save data?');"
If the user clicks "No" then the button event will never fire... You don't need any of that other stuff.
using asp.net | C#
I want my ImageButton to open a URL when I click it. I finally have my Image loading and it clicks but when I click it nothing happens. Here is the code I have so far:
aspx page
<asp:ImageButton ID="Button1" runat="server" ImageUrl="~/images/button.gif"
onclick="Open_Click"></asp:ImageButton>
aspx.cs page
protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
System.Diagnostics.Process.Start("http://www.website.com");
}
catch { }
}
You want to do a redirect, not start a process. Try this:
protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
Response.Redirect("http://www.website.com");
}
catch { }
}
Additionally, you could just set the PostBackUrl attribute on the control and have no need for a server side event.
You can do it on the client-side:
This will open in another window:
<asp:ImageButton OnClientClick="window.open('/xxx/xxx.aspx');
OR this will open in same window, javascript needs to return false so server code won't run:
<script>
function ReDirect() {
location.href = '/xxx/xxx.aspx';
return false;
}
</script>
asp:ImageButton OnClientClick="javascript:return(ReDirect());" />
I've got this button
<asp:Button runat="server" ID="btnReviewDocs" CssClass="btnReviewDocs" data-theme="b"
Text="Review Documents" OnClick="btnReviewDocs_Click" OnClientClick="clickHyperlink();"/>
And in 'OnClick' event I'm assembling an URL that I need to set to asp:Hyperlink and at the end of the 'OnClick' I'm setting this URL to the 'NavigateURL' propery of the 'asp:Hyperlink'. Once the 'asp:Hyperlink' has the correct URL I need to call the 'clickHyperlink()' function.
function clickHyperlink() {
var href = $('#hlnkID').attr('href');
if (typeof href !== "undefined") {
$.mobile.showPageLoadingMsg();
window.location.href = href;
}
}
But the 'OnClientClick' event is executed always before the 'OnClick'. Any suggestions for a workaround?
I'm doing all this stuff, because I've got problems with JQuery Mobile and 'Response.Redirect(url);' is changing the page, but not the URL.
I believe that you don't really need to involve the Hyperlink control in the JS part.
Modify your JS function and remove the OnClientClick attribute from the btnReviewDocs button:
<script type="text/javascript">
function clickHyperlink(href) {
$.mobile.showPageLoadingMsg();
window.location.href = href;
}
</script>
On the server, in the btnReviewDocs_Click method:
protected void btnReviewDocs_Click(object sender, EventArgs e)
{
// TODO: set the url, maybe append some params to the
// hlnkID.NavigateUrl value
var url = "http://stackoverflow.com/";
ClientScript.RegisterStartupScript(Page.GetType(),
"clickHyperlink",
"clickHyperlink('" + url + "');",
true);
}
Use the RegisterStartupScript in the ClientScript object to run the code after postback--->
protected void btn_Click(object sender, EventArgs e)
{
//some code
this.ClientScript.RegisterStartupScript(this.GetType(), "clintClick", "clickHyperlink", true);
}
try this
protected void btnReviewDocs_Click(object sender, EventArgs e)
{
//something doing here
Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script type='text/javascript'>clickHyperlink()</script>");//call javascript function
}
The answer is mentioned by #Alex Filipovici.
But first you should ask yourself do you really need to go back to the client side to do a redirect ?
Why not call :
Response.Redirect("MyURL");
Using C# with ASP.NET, how do I show a "success" message when my user submits a form? And at the same time say "The image has successfully saved", with a link, so that the image created can be viewed by clicking the link?
Wrap your form in an <asp:Panel> and create another <asp:Panel> with Visible="False" for your Thank you message. Once the form is submitted, change the visibility of each panel, setting the form to Visible="False", and the thank you message panel to Visible="True".
Hope that makes sense, here's an example:
<asp:Panel ID="pnlFormFields" runat="server">
... form fields here ...
</asp:Panel>
<asp:Panel ID="pnlThankYouMessage" runat="server" Visible="False">
... Thank you message here ...
</asp:Panel>
Then inside your codebehind
protected void btnSubmit_Click(object sender, EventArgs e) {
// Hook up uploaded image and assign link to it
pnlFormFields.Visible = false;
pnlThankYouMessage.Visible = true;
}
If you need label to display message. Add a label on the page and set its attribute visible = false in aspx and use the code below:
protected void btnSubmit_Click(object sender, EventArgs e) {
if(SaveRecordsToDataDatabase())
{
If(UploadImage())
{
showMessage("Save successfull",true);
}
else
{
showMessage("Save failed",false);
}
}
else
{
showMessage("Save failed",false);
}
}
private bool UploadImage()
{
// you upload image code..
}
private bool SaveRecordsToDatabase()
{
// db save code
}
private void showMessage(string message, bool success)
{
lblMsg.visible = true; // here lblMsg is asp label control on your aspx page.
lblMsg.FontBold = true;
if(success)
lblMsg.ForeColor = Color.Green;
else
lblMsg.ForeColor = Color.Green;
lblMsg.Text = message;
}
For consistency you can use Transaction in above code so as to prevent save operation if image upload fails. Otherwise its your choice. The new code with Transaction will be , given below:
protected void btnSubmit_Click(object sender, EventArgs e) {
using(TransactionScope scope = new TransactionScope())
{
if(SaveRecordsToDataDatabase())
{
If(UploadImage())
{
showMessage("Save successfull",true);
}
else
{
showMessage("Save failed",false);
}
}
else
{
showMessage("Save failed",false);
}
}
scope.complete()
}
Here to refer transaction scope, add reference System.Transactions.
İf you want to show message on client side controls, like alert("saccess");
you can use ajax and webmethod in Why doesn't my jQuery code work in Firefox and Chrome?
if you want to show message on server side you can use panel, label or div (runat server and have id) and default setting of them, set visiible false, when you show message you can set visible true via code behind..
use a label(visible=false) and a hyperlink from the toolbox .when u upload an image u must be inserting the url of savefile location to a database.so when that insert query is fired that will return an integer value which is d no of lines inserted in db.compare it like if that value > 0 then set visibily of label to true and label.text="success" finally set the navigate url of the hyperlink to d url of saved image which can be used to creat a view link of the image