 |
|
(C#) MessageBox.Show ใช้กับ Web form ไม่ได้หรอครับ ? --ต้อง using อะไรก่อนครับ |
|
 |
|
|
 |
 |
|
มันมีนะครับ แต่ไม่รู้ว่ามันแล้วแต่เหตุการณ์ที่จะใช้หรือป่าว บางทีพิมพ์ msgbox.show ก็ขึ้น บางครั้งก็ต้องพิมพ์ messagebox.show
|
 |
 |
 |
 |
Date :
2010-10-22 11:54:40 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Confirm (VB.NET)
If MessageBox.Show("Are you sure to exit?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Application.Exit()
End If
Message Box (VB.NET)
MessageBox.Show("Please input (Name)")
|
 |
 |
 |
 |
Date :
2010-10-22 12:01:07 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
MessageBox.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text;
/// <summary>
/// Summary description for MessageBox
/// </summary>
public class MessageBox
{
private static Hashtable m_executingPages = new Hashtable();
public MessageBox()
{
//
// TODO: Add constructor logic here
//
}
public static void Show(string sMessage)
{
// If this is the first time a page has called this method then
if(!m_executingPages.Contains(HttpContext.Current.Handler))
{
// Attempt to cast HttpHandler as a Page.
Page executingPage = HttpContext.Current.Handler as Page;
if(executingPage != null)
{
// Create a Queue to hold one or more messages.
Queue messageQueue = new Queue();
// Add our message to the Queue
messageQueue.Enqueue(sMessage);
// Add our message queue to the hash table. Use our page reference
// (IHttpHandler) as the key.
m_executingPages.Add(HttpContext.Current.Handler, messageQueue);
// Wire up Unload event so that we can inject
// some JavaScript for the alerts.
executingPage.Unload += new EventHandler(ExecutingPage_Unload);
}
}
else
{
// If were here then the method has allready been
// called from the executing Page.
// We have allready created a message queue and stored a
// reference to it in our hastable.
Queue queue = (Queue) m_executingPages[HttpContext.Current.Handler];
// Add our message to the Queue
queue.Enqueue(sMessage);
}
}
// Our page has finished rendering so lets output the
// JavaScript to produce the alert's
private static void ExecutingPage_Unload(object sender, EventArgs e)
{
// Get our message queue from the hashtable
Queue queue = (Queue) m_executingPages[HttpContext.Current.Handler];
if(queue != null)
{
StringBuilder sb = new StringBuilder();
// How many messages have been registered?
int iMsgCount = queue.Count;
// Use StringBuilder to build up our client slide JavaScript.
sb.Append("<script language='javascript'>");
// Loop round registered messages
string sMsg;
while(iMsgCount-- > 0)
{
sMsg = (string) queue.Dequeue();
sMsg = sMsg.Replace( "\n", "\\n" );
sMsg = sMsg.Replace( "\"", "'" );
sb.Append( @"alert( """ + sMsg + @""" );" );
}
// Close our JS
sb.Append(@"</script>");
// Were done, so remove our page reference from the hashtable
m_executingPages.Remove(HttpContext.Current.Handler);
// Write the JavaScript to the end of the response stream.
HttpContext.Current.Response.Write(sb.ToString());
}
}
}
คลิกขวาที่ folder app_code แล้ว add new item เป็น class แล้วก็อปด้านบนไปใส่
ไปปลูกผักต่อแล้ว
|
 |
 |
 |
 |
Date :
2010-10-22 12:50:07 |
By :
ตังค์แมน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โห
สรุปคือแค่ MessageBox นี่ C# ต้องเขียน class ขึ้นมาเองเลยหรอครับ ><
|
 |
 |
 |
 |
Date :
2010-10-22 13:17:31 |
By :
zixsenz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จริงแล้วใช้ MsgBox.Show ก็ได้เหมือนกันครับ แต่ที่ไม่ขึ้นี้ผมว่า คุณเขียนยังไงถึงไม่ขึ้นมากกว่า
|
 |
 |
 |
 |
Date :
2010-10-22 13:24:53 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถูกครับ VS(C#) บางตัวจะไม่มี MsgBox ให้เรียกใช้งาน
ลองค้นหากระทู้เก่าๆดูครับ อาจมีคำตอบให้คุณได้
|
 |
 |
 |
 |
Date :
2010-10-22 16:45:49 |
By :
3rds |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ javascript ดีกว่านะครับ
Response.Write("<script type="text/javascript">alert('test');</script>");
|
 |
 |
 |
 |
Date :
2010-10-22 18:53:56 |
By :
เราเอง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียน MSG บน webform ไม่เหมือนเขียนบน windown form น่ะครับ
|
 |
 |
 |
 |
Date :
2011-09-30 11:37:45 |
By :
Noomyontrakit |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
lberror.Text = "กรุณาตรวจสอบข้อมูล";
lberror.Visible = true;
|
 |
 |
 |
 |
Date :
2012-04-18 16:42:45 |
By :
a |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ฮ่วย มันจะไปเหมือน msg ของ win form ได้ไงเล่า
class นี้ทำให้สามารถใช้ javascript alert message แบบง่ายๆ โดยไม่ต้องไปเขียน javascipt ยาวๆ
โดยให้วิธีการใช้งานคล้ายๆ ของ win form แค่นั้นแหละ โดยความสามารถก็แค่ alert message
เช่น
Code (C#)
protected void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("แสดง alert");
}
ไม่ถึงกับเหมือน messagebox จริงๆ หรอก ถ้าจะให้ได้แบบนั้นต้องใช้ popup model แล้ว
|
 |
 |
 |
 |
Date :
2013-03-06 14:16:19 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|