How to call a conditional confirm box from c#.
I have 2 hidden fields and based on the condition I want to call confirm box.
After that I also want what user has pressed (clicked on yes or No).
Design:-
<input type="submit" id="btnAddPaymentMethod" onserverclick="AddPaymentMethod_Click" runat="server" value="add payment method" />
Code:-
protected void Next_Click(object sender, EventArgs e)
{
if (hdnDefault.Value == hdnPrimary.Value) { return; }
else
{
//open confirm box
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "confirm('Do you want to save new default payment method?');", true);
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
}
}
I have tried below jQuery Code:-
function Confirm(msg) {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm(msg)) {
confirm_value.value = "Yes";
$('#btnAddPaymentMethod').click();
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
protected void Next_Click(object sender, EventArgs e)
{
if (hdnDefault.Value == hdnPrimary.Value) {
return;
} else {
//open confirm box
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "Confirm('Do you want to save new default payment method?');", true);
}
}
protected void AddPaymentMethod_Click(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes") {
ScriptManager.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
} else {
ScriptManager.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
}
function Confirm(msg) {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
confirm_value.value = confirm(msg)? "Yes" : "No";
document.forms[0].appendChild(confirm_value);
$('#btnAddPaymentMethod').click();
}
I have not run your code. But when you have runat="server" for an input control it will append with the asp.net unique Id. So try to access the input control by it's name ending with btnAddPaymentMethod as below.
Change from $('#btnAddPaymentMethod').click(); to $('[id$=btnAddPaymentMethod]').click();
This jQuery code will open a confirm dialog, with an 'ok' and 'cancel' buttons.
Here an anchor tag with an id of myConfirmPageLink, when clicked, will ask for confirmation. If ok is clicked it proceeds to the target, and if cancel is clicked it stays on the same page.
$("a#myConfirmPageLink").click(function(){
return confirm("Are you sure you want to go to that page/site ?");
});
This should be easy to modify for your purposes.
Related
I am currently developing a DotNetNuke module. However, I failed to prompt user an alert dialog box in certain situations like record duplication.
I am using the following code to display an alert box in Controller class.
EditForm edForm = new EditForm();
ScriptManager.RegisterClientScriptBlock(edForm, edForm.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
The following is my full code.
Form.ascx.cs
void cmdUpdate_Click(object sender, EventArgs e)
{
UdtController.UpdateRow(Data, ModuleId, False);
}
UdtController.cs
public void UpdateRow(DataSet ds, int rowNr, bool isDataToImport)
{
var values = new Dictionary<int, string>();
string strIsUnique = "";
foreach (DataRow field in ds.Tables[DataSetTableName.Fields].Rows)
{
var strColumnName = field[FieldsTableColumn.Title].ToString();
strIsUnique = field[FieldsTableColumn.Searchable].ToString();
var strValueColumn = ((!isDataToImport &&
ds.Tables[DataSetTableName.Data].Columns.Contains(strColumnName +
DataTableColumn.
Appendix_Original))
? strColumnName + DataTableColumn.Appendix_Original
: strColumnName);
if (strIsUnique == "True")
{
int uniqueDataCount = FieldController.uniqueData(currentRow[strValueColumn].AsString());
if (uniqueDataCount == 0)
{
if (ds.Tables[DataSetTableName.Data].Columns.Contains(strValueColumn))
{
values.Add(field[FieldsTableColumn.Id].AsInt(), currentRow[strValueColumn].AsString());
}
}
else
{
EditForm edForm = new EditForm();
ScriptManager.RegisterClientScriptBlock(edForm, edForm.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
break;
}
}
else
{
if (ds.Tables[DataSetTableName.Data].Columns.Contains(strValueColumn))
{
values.Add(field[FieldsTableColumn.Id].AsInt(), currentRow[strValueColumn].AsString());
}
}
}
FieldController.UpdateData(userDefinedRowId, values);
}
You need to reference the Page, not create a new form.
Page page = HttpContext.Current.CurrentHandler as Page;
ScriptManager.RegisterStartupScript(page, page.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
However DNN has it's own message box you could use:
http://uxguide.dotnetnuke.com/UIPatterns/AlertDialog.html
I have two forms i.e., frmLogin and frmDash. I have username and password saved in credentials.txt file. My default run form is frmLogin. Now my problem is, when application starts it checks username and password from credentials.txtand directly shows frmDash. Its working, but problem is, with frmDash , frmLogin is also opening at back. How to solve this?
I have tried this(Form1 is frmLogin):
private void Form1_Load(object sender, EventArgs e)
{
try
{
var credentialLines = File.ReadAllLines(Environment.CurrentDirectory + "\\credentials\\credentials.txt");
if (credentialLines.Any())
{
UserName_reLogin = credentialLines[0];
Password_reLogin = credentialLines[1];
if (LoginUser(Log_API, UserName_reLogin, Password_reLogin))
{
logIn_Status = "true";
GlolbalUtil.LogIn_Status = logIn_Status;
//this.Hide();
frmDash frmDash = new frmDash();
frmDash.Owner = this;
frmDash.Show();
txtUsername.Text = "";
txtPassword.Text = "";
//GlolbalUtil.accept_status = "1";
this.Enabled = false;
}
else
{
MessageBox.Show("Please Check Username and password");
}
}
else
{
this.Enabled = true;
}
}
catch
{
}
}
Move to your login logic to Program.cs in the Main function for something like this
var credentialLines = File.ReadAllLines(Environment.CurrentDirectory + "\\credentials\\credentials.txt");
if (credentialLines.Any()){
UserName_reLogin = credentialLines[0];
Password_reLogin = credentialLines[1];
if (LoginUser(Log_API, UserName_reLogin, Password_reLogin)){
Application.Run(new frmDash ());
}else{
Application.Run(new frmlogin());
}
}else
{
Application.Run(new frmlogin());
}
First of all, you should check if the credential exists before opening the login Form.
but anyway to hide forms use this.Hide(); to hide forms
private void FrmLogin_Shown(object sender, EventArgs e)
{
if (GlolbalUtil.authenticate == "true")
{
this.Hide();
}
else if(GlolbalUtil.authenticate == "false")
{
this.Show();
}
}
GlobalUtil.authenticate is global variable to check if user is logged in or not. if user is logged in that means GlobalUtil.authenticate=="true", then only frmLogin will hide otherwise show. worked perfectly.
This is the script and I want to use it in code behind previously I used clentclick property of button button I want to use this code without using button
<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>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
what else can I do so that it is achievable for me
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
// TextBox1.Attributes.Add("OnClientClick", "Confirm()");
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
//Your logic for OK button
}
else
{
//Your logic for cancel button
}
}
public void OnConfirm(object sender, EventArgs e)
{
}
Leave a button on the page, hide it using css and then call click() from JavaScript after you have set your value:
<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);
document.getElementById("Button1").click();
}
</script>
<asp:TextBox ID="TextBox1" runat="server" onchange="Confirm()"></asp:TextBox>
<div style="display:none;"><asp:Button ID="Button1" ClientIDMode="static" runat="server" onclick="Button1_Clicked" /></div>
Then in your codebehind:
protected void Button1_Clicked(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
//Your logic for OK button
}
else
{
//Your logic for cancel button
}
}
protected void Page_Load(object sender, EventArgs e)
{
Button cmdTemp = null;
try
{
cmdTemp = (Button)GetPostBackControl(this);
}
catch { }
FillTableDB();
if(IsPostBack)
{
if(cmdTemp == null || cmdTemp.ID == "btnNew" || cmdTemp.ID != "btnSave")
{
GenerateBlankTableHtml("");
}
}
}
private void FillTableDB()
{
//SQL QUERY
//Select status from table
GenerateBlankTableHtml(status)
}
private void GenerateBlankTableHtml(string status)
{
if(status=="")
{
btnNew.Style.Add("Display", "none");
}
else
{
//show status in label
lblStatus.text=status;
}
}
public static Control GetPostBackControl(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if(ctrlname != null && ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach(string ctl in page.Request.Form)
{
Control c = page.FindControl(ctl);
if(c is System.Web.UI.WebControls.Button)
{
control = c;
break;
}
}
}
return control;
}
ASPX:
<asp:Button ID="btnSave" runat="server"/>
<asp:Button ID="btnNew" runat="server"/>
<asp:Label ID="lblStatus" runat="server"
I have two functions FillTableDB();GenerateBlankTableHtml(string status);
When status getting blank i have to hide btnNew otherwise showing status in label.
if label having status then and only then New study botton will displayed otherwise not.
What i want when user click on button NEW then and only then i have to show label text with blank status Not click on save button What should i do.
Try this
if(IsPostBack)
{
if(btnNew.Style.Value == "Display:none;")
{
GenerateBlankTableHtml("");
}
}
protected void btnNew_Click(object sender, EventArgs e)
{
GenerateBlankTableHtml("");
}
do something like following.
<asp:Button ID="btnNew" runat="server" onClick="btnNew_click"/>
and now on that button new click.
protected void btnNew_Click(object sender, EventArgs e)
{
Button btnNew = (Button)sender;
btnNew.Style.Add("Display", "none");
lblStatus.text = string.empty;
}
When the user clicks on the "Export to Excel" link, the standard "File download" dialog is presented to the user. See here for an example image.
But before exporting the excel file, I want to display an alert popup. But the Save dialog is obscuring the view of the alert popup.
How can I display the popup without it being obscured?
Here is my code...
dsResult = clsObj.getSearchResults_BL(detObj);
if (OrdDifference != null && OrdDifference.Any())
{
ScriptManager.RegisterClientScriptBlock(this.up, this.GetType(), "export", "alert('.....')", true);
set(dsResult, strName);
}
else
{
set(dsResult, strName);
}
private void set(DataSet ds, string strFileName)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2007;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
try
{
sheet.Name = strFileName;
sheet.ImportDataTable(ds.Tables[0], true, 1, 1, -1, -1);
...
workbook.SaveAs(strFileName, ExcelSaveType.SaveAsXLS, HttpContext.Current.Response, ExcelDownloadType.PromptDialog);
}
catch (Exception ex)
{
}
}
Your problem is here:
ScriptManager.RegisterClientScriptBlock(this.up, this.GetType(), "export", "alert('.....')", true);
set(dsResult, strName);
Because the set method in your program is writing to the Response Stream the call to ScriptManager.RegisterClientScriptBlock ends up doing nothing.
You need to do this on two steps:
if (OrdDifference != null && OrdDifference.Any())
{
//Just do this, nothing more.
ScriptManager.RegisterClientScriptBlock(this.up, this.GetType(), "export", "alertUser('Some Message Here')", true);
}
Now define the alertUser function in Javascript:
function alertUser(message)
{
alert(message);
window.location='AccepServiceOrder.aspx?o=Export';
}
Now on Page_Load check for the o parameter in the query string
protected void Page_Load(object sender, EventArgs e)
{
if(Request.QueryString["o"]!=null)
{
dsResult = clsObj.getSearchResults_BL(detObj);
set(dsResult, strName);
}
}