Toast notification is not displaying on a web page - c#

My code is as follows:
void CallToast(String msg, String cls)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("toast(" + msg + "," + cls + ");");
sb.Append("</script>");
ScriptManager.RegisterStartupScript(this, this.GetType(), "ajax", sb.ToString(), false);
}
protected void Button1_Click(object sender, EventArgs e)
{
CallToast("Oops... Some thing is wrong...", "toast-error");
}
AND JAVASRICPT FUNCTION IS :
<script type="text/javascript">
function toast(sMessage, sIco) {
if ($('.toast').length <= 0) {
var container = $(document.createElement("div"));
container.addClass("toast");
container.appendTo(document.body);
}
var message = $(document.createElement("div"));
message.addClass(sIco);
message.text(sMessage);
message.appendTo($('.toast'));
message.delay(100).fadeIn("slow", function () {
$(this).delay(5000).fadeOut("slow", function () {
$(this).remove();
});
});
}
What is the problem? notification is not seen on a button click. i hv written css file.

Your javascript function name is toast ?
Try this one ,
ScriptManager.RegisterStartupScript(this, this.GetType(), "toast()", sb.ToString(), false);

Related

DotNetNuke WebForm Alert Dialog box

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

Conditional confirm box in c#

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.

C# ASP.NET Open a new windows and redirecting in button click event

How can i redirect to another page and open a new window in one button click?
protected void ASPxButton_save_Click(object sender, EventArgs e)
{
try
{
//do other stuff
if (oSoins.DEVENIR_ECVAC_PAR != 0)
{
string AdrUrl = "Print_DosSoin.aspx?SoinId=" + Soin_Id;
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}');</script>", AdrUrl));
}
Response.Redirect("DossierSoin_Liste.aspx");
}
catch (ThreadAbortException) {/* do nothing, it might be a response.Redirect exception */}
catch (Exception excThrown)
{
lbl_err.Text = excThrown.Message;
if (excThrown.InnerException != null) lbl_err.Text += "-->" + excThrown.InnerException.Message;
string TempMsg = "DosSoin Fiche " + Appel_ID + "-- ASPxButton_save_Click -->" + lbl_err.Text;
Outils.SendingEmail(TempMsg);
}
With the script above it's redirecting but does not open a new window.
Thank you in advance.
You should modify your javascript to the following:
string script = default(string)
if (oSoins.DEVENIR_ECVAC_PAR != 0)
{
string AdrUrl = "Print_DosSoin.aspx?SoinId=" + Soin_Id;
script = string.Format("window.open('{0}'); ", AdrUrl);
}
script += " window.location.href = 'DossierSoin_Liste.aspx';";
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", script, true);
This will create a new window then redirect on the client.

passing values (through listbox) between webpages in asp.net-c#

~/Admin/AdimHome.aspx.cs C# code
protected void Page_Load(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language='javascript'>function Open() {");
sb.Append(string.Format("window.open('Chat.aspx?rid={0}'", lstRooms.SelectedValue));
sb.Append(", 'newwindow','toolbar=no,location=no,menubar=no,width=290,height=330,resizable=no,scrollbars=no,top=350,left=980,right=500');return false;");
sb.Append("}</script>");
if (!ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "JSScriptBlock", sb.ToString());
}
lblFacultyNo.Text = Session["User_Id"].ToString();
lblUserType.Text = Session["User_Type"].ToString();
pnlChat.Visible = false;
}
~/Admin/Chat.aspx.cs page C# code
protected void Page_Load(object sender, EventArgs e)
{
if (Session["User_Id"] == null)
Response.Redirect("~/Admin/AdimHome.aspx");
if (string.IsNullOrEmpty(Request.QueryString["rid"]))
Response.Redirect("~/Admin/AdminHome.aspx");
txtMsg.Attributes.Add("onkeypress", "return clickButton(event,'btn')");
if (!IsPostBack)
{
hdnRoomID.Value = Request.QueryString["rid"];
ChatRoom room = ChatEngine.GetRoom(hdnRoomID.Value);
string prevMsgs = room.JoinRoom(Session["User_Id"].ToString(), Session["User_Id"].ToString());
txt.Text = prevMsgs;
foreach (string s in room.GetRoomUsersNames())
{
lstMembers.Items.Add(new ListItem(s, s));
}
}
}
want to pass lstRooms.SelectedValue to Chat.aspx.cs page to check as per client request to differentiate their chat rooms:
sb.Append(string.Format("window.open('Chat.aspx?rid={0}'", lstRooms.SelectedValue));
onclicking the btnChat event:
<asp:Button ID="btnChat" Runat="server" CssClass="btn" OnClientClick="JavaScript:Open()" OnClick="btnChat_Click" Text="Join Room" />
The simple solution to your problem could be if you want to change your code...
//Javascript function
function Open()
{
var ddl = document.getElementbyId('<%= lstRooms.ClientID%>');
var ddlvalue = ddl.options[ddl.selectedIndex].value;
Window.Open("Chat.aspx?rid=" + ddlvalue );
}
remove all the code for JS in pageload and put this on aspx page.
let me know if it solves

How to use ScriptManager in class file?

I Have a common method that displays alert message using page.clientScript. But later on i added update panel. Now this piece of code is not working. So I need to call there scriptmanager, but i get some error message that it is accessible there.
Below is my ShowMessage method of common.cs file
private static void ShowMessage(Page currentPage, string message)
{
var sb = new StringBuilder();
sb.Append("alert('");
sb.Append(message);
sb.Append("');");
currentPage.ClientScript.RegisterClientScriptBlock(typeof(Common), "showalert", sb.ToString(), true);
}
So how do I use this method under update panel
Make use of : ScriptManager.RegisterClientScriptBlock Method
ScriptManager.RegisterClientScriptBlock(
this,
typeof(Page),
"TScript",
script,
true);
const string scriptString = "<script type='text/javascript'> alert('message');</script>";
ClientScriptManager script = Page.ClientScript;
script.RegisterClientScriptBlock(GetType(), "randomName", scriptString);
For use in a class file:
public static void SendAlert(string sMessage)
{
sMessage = "alert('" + sMessage.Replace("'", #"\'").Replace("\n", #"\n") + "');";
if (HttpContext.Current.CurrentHandler is Page)
{
Page p = (Page)HttpContext.Current.CurrentHandler;
if (ScriptManager.GetCurrent(p) != null)
{
ScriptManager.RegisterStartupScript(p, typeof(Page), "Message", sMessage, true);
}
else
{
p.ClientScript.RegisterStartupScript(typeof(Page), "Message", sMessage, true);
}
}
}
This could be expanded to include other possible handlers, but for the moment this is how I solved the problem.
Try this in .cs file
var page = HttpContext.Current.CurrentHandler as Page;
ScriptManager.RegisterStartupScript(page, page.GetType(), "alert", "alert('Success');window.location ='Home.aspx';", true);
It's working for me ^^
Here's how I did it:
public partial class JQuery
{
private Page page;
public JQuery(Page pagina) {
page = pagina;
}
public void Alert(string Title, string Message)
{
Message = Message.Replace("\n", "<br>");
string command = String.Format("myCustomDialog('{0}','{1}')", Title, Message);
ScriptManager.RegisterClientScriptBlock(page, this.GetType(), "", command, true);
}
}
Then you can use like this:
JQuery jquery = new JQuery(this.Page);
jQuery.Alert("Title", "Look, a jQuery dialog!");

Categories