2

ユーザー名のテキストボックスと、クリックするとユーザーの詳細を表示する更新ボタンがある管理ページを作成しています。他のコントロールの中でも、ユーザーを削除するための「ユーザーの削除」ボタンがあります。これを試みる前に確認を取得するためのJavaScriptコードがあります。この [ユーザーの削除] ボタンに送信動作を持たせたくないので、UseSubmitBehavior=False に設定しました。ただし、これによりサーバー側のイベントが発生しませんでした。そこで、__doPostBack を明示的に呼び出す小さなクライアント側関数を作成しました。

最終的なコードは機能しますが、マスター ページまたはネストされたマスター ページに基づいていないページでのみ機能します。悲しいことに、私の Web ページはすべてマスター ページに基づいています。私が何かを見逃しているのか、それとも回避策があるのか​​ 疑問に思っていますか? asp.net を始めたばかりなので、明らかな間違いがあればご容赦ください。

よろしくお願いします。

コードサンプル:

次のコードが機能します (マスター ページのない Web フォーム)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm_with_NoMasterPage.aspx.cs" Inherits="WebApplication1.WebForm_with_NoMasterPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <script type="text/javascript">
                function GetConfirmation(btn, msg) {
                    if (confirm(msg)) {
                        __doPostBack(btn.id, '');
                        return true;
                    }
                    else {
                        return false;
                    }
                }
            </script>

            <asp:Button ID="btnRemoveUser" runat="server" Text="Remove User" OnClick="btnRemoveUser_Click" 
                OnClientClick="return GetConfirmation(btnRemoveUser, 'Really remove?');"
                UseSubmitBehavior="false"
                Height="37px" Width="230px" />

    </div>
    </form>
</body>
</html>

コードビハインド:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm_with_NoMasterPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRemoveUser_Click(object sender, EventArgs e)
        {
            // put a breakpoint here and see if it is hit.
            int x = 0;
        }
    }
}

以下は機能しません (同じフラグメントですが、マスターページに基づく Web フォーム内):

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm_WITH_MasterPage.aspx.cs" Inherits="WebApplication1.WebForm_WITH_MasterPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
            <script type="text/javascript">
                function GetConfirmation(btn, msg) {
                    if (confirm(msg)) {
                        __doPostBack(btn.id, '');
                        return true;
                    }
                    else {
                        return false;
                    }
                }
            </script>

            <asp:Button ID="btnRemoveUser" runat="server" Text="Remove User" OnClick="btnRemoveUser_Click" 
                OnClientClick="return GetConfirmation(btnRemoveUser, 'Really remove?');"
                UseSubmitBehavior="false"
                Height="37px" Width="230px" />

</asp:Content>

コードビハインド:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm_WITH_MasterPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRemoveUser_Click(object sender, EventArgs e)
        {
            // put a breakpoint here and see if it is hit
            int z = 0;
        }
    }
}
4

0 に答える 0