ผู้ตอบมากที่สุด
ASP.NET Navigation controls FAQ: วิธีการเพิ่มข้อความยืนยัน (confirmation dialog) ในแต่ละ TreeNode

คำถาม
-
ASP.NET Navigation controls FAQ: วิธีการเพิ่มข้อความยืนยัน (confirmation dialog) ในแต่ละ TreeNode
Supa Sethasiripong [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
คำตอบ
-
โดยปกติ เวลาที่เราคลิกที่ TreeNode หน้าปัจจุบันจะถูกเปลี่ยนไปเป็นหน้าอื่นได้โดยตรง
แต่ถ้าเราต้องการให้มีpopup ยืนยันข้อความก่อนที่จะเปลี่ยนหน้าไปเป็นหน้าอื่น
เราสามารถใช้โค้ดดังนี้
<%@PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
protectedvoidPage_Load(objectsender,EventArgse)
{
stringconfirmMessage = "Yes/No?";
// write a function for popup a confirm dialog
stringscript = @"function treeNodeConfirmation(mEvent, text)
{
var o;
// Internet Explorer
if (mEvent.srcElement)
{
o = mEvent.srcElement;
}
// Netscape and Firefox
else if (mEvent.target)
{
o = mEvent.target;
}
if(o.tagName == 'A' || o.tagName == 'a')
{
return confirm (text);
}
}";
// regist the function
ScriptManager.RegisterClientScriptBlock(myTreeView,typeof(TreeView),"treeNodeClickConfirm", script, true);
// add the function to the TreeView’s Attributes
// it means when the client users click the TreeNodes, it will popup a confirm// dialog.
myTreeView.Attributes.Add("onclick","javascript:return treeNodeConfirmation(event, '" + confirmMessage +"')");
}
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>Demo</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:TreeViewID="myTreeView"runat="server">
<Nodes>
<asp:TreeNodeText="My Computer">
<asp:TreeNodeText="Favorites">
<asp:TreeNodeText="News">
<asp:TreeNodeText="MSN"NavigateUrl="http://www.msn.com"/>
<asp:TreeNodeText="MSNBC News"NavigateUrl="http://www.msnbc.msn.com"/>
</asp:TreeNode>
<asp:TreeNodeText="Technology">
<asp:TreeNodeText="Microsoft"NavigateUrl="http://www.microsoft.com"/>
<asp:TreeNodeText="ASP.NET"NavigateUrl="http://www.asp.net"/>
<asp:TreeNodeText="GotDotNet"NavigateUrl="http://www.gotdotnet.com"/>
<asp:TreeNodeText="MSDN"NavigateUrl="http://msdn.microsoft.com"/>
</asp:TreeNode>
<asp:TreeNodeText="Shopping">
<asp:TreeNodeText="MSN Shopping"NavigateUrl="http://shopping.msn.com"/>
<asp:TreeNodeText="MSN Autos"NavigateUrl="http://autos.msn.com"/>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNodeText="City Links">
<asp:TreeNodeText="MapPoint"NavigateUrl="http://www.mappoint.com"/>
<asp:TreeNodeText="MSN City Guides"NavigateUrl="http://local.msn.com"/>
</asp:TreeNode>
<asp:TreeNodeText="Music Links">
<asp:TreeNodeText="MSN Music"NavigateUrl="http://music.msn.com"/>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</div>
</form>
</body>
</html>
ฟอรัมที่เกี่ยวข้อง
http://forums.asp.net/t/1020789.aspx
http://forums.asp.net/t/1355481.aspx
http://forums.asp.net/p/1353925/2771697.aspx
http://forums.asp.net/p/1353925/2790985.aspx
Supa Sethasiripong [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- ทำเครื่องหมายเป็นคำตอบโดย supa_sModerator 30 มีนาคม 2555 6:09