Sto tentando di visualizzare un messaggio toastr (informazioni, errore, ecc.) Dopo aver inviato un modulo utilizzando un pulsante e aggiornato un controllo di griglia (che si trova in un pannello di aggiornamento "nel webform di asp.net. . grazieAggiunta di toastr javascript asp.net webform
risposta
è possibile farlo utilizzando Page.ClientScript.RegisterStartupScript
metodo Esempio:
Page.ClientScript.RegisterStartupScript(this.GetType(),
"toastr_message", "toastr.error('There was an error', 'Error')", true);
ma probabilmente creare un metodo metodo o estensione per gestire questo per me:
public static void ShowToastr(this Page page, string message, string title, string type = "info")
{
page.ClientScript.RegisterStartupScript(page.GetType(), "toastr_message",
String.Format("toastr.{0}('{1}', '{2}');", type.ToLower(), message, title), addScriptTags: true);
}
Usa:
ShowToastr(this.Page, "Hello world!", "Hello");
Se volete qualcosa di un po 'più robusto, si potrebbe creare il type
parametro di un enum
.
Penso che sia pulito e molto robusto ... Grazie – dblay
Chiamata da modulo web, (notare che questo è una forma MasterDetail quindi ha una MasterPage.
MasterPage.ShowToastr (pagina, "messaggio qui", "Title Here", "Info", False, "Toast bottom-full-width", false)
l'implementazione VB.NET ShowToastr nella pagina master (VB)
Public Shared Sub ShowToastr(ByVal page As Page, ByVal message As String, ByVal title As String, Optional ByVal type As String = "info", Optional ByVal clearToast As Boolean = False, Optional ByVal pos As String = "toast-top-left", Optional ByVal Sticky As Boolean = False)
Dim toastrScript As String = [String].Format("Notify('{0}','{1}','{2}', '{3}', '{4}', '{5}');", message, title, type, clearToast, pos, Sticky)
page.ClientScript.RegisterStartupScript(page.[GetType](), "toastr_message", toastrScript, addScriptTags:=True)
End Sub
la funzione ShowToastr Javascript risiede nella pagina master come una funzione condivisa.
<link href="./Media/css/Grey/ListBox.Grey.css" rel="stylesheet" type="text/css" />
<link href="./Media/css/WebTrack.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css"
rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"
type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function Notify(msg, title, type, clear, pos, sticky) {
// toastr.options.positionClass = "toast-bottom-right";
// toastr.options.positionClass = "toast-bottom-left";
// toastr.options.positionClass = "toast-top-right";
// toastr.options.positionClass = "toast-top-left";
// toastr.options.positionClass = "toast-bottom-full-width";
// toastr.options.positionClass = "toast-top-full-width";
// options = {
// tapToDismiss: true,
// toastClass: 'toast',
// containerId: 'toast-container',
// debug: false,
// fadeIn: 300,
// fadeOut: 1000,
// extendedTimeOut: 1000,
// iconClass: 'toast-info',
// positionClass: 'toast-top-right',
// timeOut: 5000, // Set timeOut to 0 to make it sticky
// titleClass: 'toast-title',
// messageClass: 'toast-message' }
if (clear == true) {
toastr.clear();
}
if (sticky == true) {
toastr.tapToDismiss = true;
toastr.timeOut = 10000;
}
toastr.options.onclick = function() {
//alert('You can perform some custom action after a toast goes away');
}
//"toast-top-left";
toastr.options.positionClass = pos;
if (type.toLowerCase() == 'info') {
toastr.options.timeOut = 1000;
toastr.tapToDismiss = true;
toastr.info(msg, title);
}
if (type.toLowerCase() == 'success') {
toastr.options.timeOut = 1500;
toastr.success(msg, title);
}
if (type.toLowerCase() == 'warning') {
toastr.options.timeOut = 3000;
toastr.warning(msg, title);
}
if (type.toLowerCase() == 'error') {
toastr.options.timeOut = 10000;
toastr.error(msg, title);
}
}
</script>
Spero che questo aiuti qualcuno, come ho cercato per anni per ottenere le opzioni toastr integrate in una chiamata. Se desideri avere più opzioni disponibili sulla chiamata a toastr, quindi aggiungi altri parametri alle funzioni. Tutte le opzioni che possono essere impostate sono nel codice commentato (javascript).
- 1. ASP.NET Webform, JavaScript nei controlli utente
- 2. jQueryMobile in ASP.NET WebForm
- 3. come ottenere js POST in asp.net webform?
- 4. Asp.net Webform Display Avviso e reindirizzamento
- 5. ASP.Net Core 1.0 supporta progetti WebForm
- 6. ASP.NET MVC è destinato a sostituire Webform?
- 7. Come utilizzare l'etichetta in un webform ASP.Net?
- 8. Come prevenire l'attacco CSRF nel webform di asp.net?
- 9. ASP.NET MVC Dovrei preoccuparmi di imparare prima i Webform?
- 10. Icone duplicate con toastr
- 11. Impostazione dell'opac toastr?
- 12. JavaScript Aggiunta di booleani
- 13. Accesso a HtmlHelpers da WebForm quando si utilizza ASP.NET MVC
- 14. "Aggiungi controller"/"Aggiungi vista" in un'applicazione ibrida MVC/WebForm ASP.NET
- 15. Toastr JS Impostazione valore Fadeaway
- 16. ASP.NET: aggiunta controlli lato client
- 17. Filosofia MVC applicata a webform
- 18. Aggiunta di un file JavaScript
- 19. asp.net mvc Aggiunta all'attributo AUTHORIZE
- 20. Come ottenere toastr per funzionare in Typescript
- 21. ENTITY/LINQ/ASP.NET: ASPxGridView Aggiunta di regole
- 22. Iscrizione ASP.net - aggiunta di un ruolo
- 23. Aggiunta di intestazioni in ASP.NET MVC 3
- 24. Aggiunta di Funscript a un progetto MVC ASP.NET esistente
- 25. Utilizzo di MVC HtmlHelper da un WebForm
- 26. Gli avvisi di toastr sembrano avvisi di bootstrap
- 27. Disattivazione bundling/minification durante il debug in WebForm
- 28. Riguardo al routing di asp.net nel webform e nel segnale Asterisk
- 29. WebForm ASP.NET - Come impostare colspan per la riga di intestazione in GridView?
- 30. Come usare async/attendere per ottenere una pagina asincrona nel webform di asp.net?
Appena preso; ScriptManager.RegisterStartupScript (questo, GetType(), ", toastr", "toastr.info ('Cliente aggiunto', 'Messaggio')", vero); – dblay
Approccio interessante con ScriptManager ... pulito! –