0

リピーターのリンクボタンがクリックされ、何らかの条件が満たされるたびに、javascript確認ボックスを表示しようとしています。

私のリピーターは、Ajax アコーディオン コンテンツ テンプレート内にある更新パネル内にあります。

<Accordion><UpdatePanel><Repeater>this is where the link button is</Repeater></UpdatePanel></Accordion>

ここに私のコードがあります、私はそれを Repeater_ItemCommand メソッドの中に入れました:

 Page.ClientScript.RegisterStartupScript(this.GetType(), "ConfirmDelete", "return confirm('Are You Sure Want To Delete?');");

いくつかのコード ソリューションを調べましたが、まだ機能していません。私は何を間違えましたか?

アップデート:

htmlからjavascriptを呼び出すと. それはうまく動作します。コード:

<asp:LinkButton ID="lbDelete" runat="server" OnClientClick="return confirm('Are you sure want to delete?');">Delete</asp:LinkButton>

しかし、私はまだコードビハインドからそれを呼び出すことができません

4

6 に答える 6

1

この方法でリピーターにjsを配置する必要があります。

<asp:Repeater runat="server" ID="rep">
    <ItemTemplate>
        <asp:Button runat="server" ID="btnDelete" CommandName="ConfirmDelete" OnClientClick="return confirm('Are You Sure Want To Delete?');" />
    </ItemTemplate>
</asp:Repeater>

また、データバインド中IDに、ボタンのCommandArgumentプロパティにを設定する必要があります。これにより、リピーターのitemコマンドで、どれを削除するかを知ることができます。

于 2013-03-07T05:17:18.937 に答える
1

Repeater_ItemDataBound イベントを使用して実現できます。コントロールがバインドされているときに、JavaScript をボタン/リンクボタンに登録するだけです。次のコードブロックは大まかなアイデアを提供します

    protected void rpt_ItemDataBound(object source, RepeaterCommandEventArgs e)
    {
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==  ListItemType.AlternatingItem)
       {
          LinkButton btn = (LinkButton)e.Item.FindControl("btnDelete");
          btn.Attributes.Add("onclick", "if ( ! confirm( 'Delete this record?' )) return false; ");

       } 
    }

これは、コントロールが ajax アコーディオン パネルにある場合でも機能します。

このようにして、スクリプトはアイテム テンプレートの各リンクボタン アイテムにバインドされます。このコードは、Item または AlternatingItem 行を検索し、FindControl メソッドを使用して btnDelete コントロールを見つけてから、onclick 属性を Attributes コレクションに追加します。その結果、ユーザーが [リンクの削除] ボタンをクリックすると、削除を確認する [OK/キャンセル] ダイアログが表示されます。

次の参照リンクを使用して、同じことに関する詳細情報を見つけることができます

参照

于 2013-03-07T05:46:25.117 に答える
1

これを試して:

page.RegisterStartupScript(this, this.GetType(), "confirmDelete", "return confirm('confirm?\.');", true);

これもチェックしてください:http://forums.asp.net/t/1363887.aspx

于 2013-03-07T04:52:47.937 に答える
1

これを試してください (4 番目のパラメーターを に設定してtrue):

Page.ClientScript.RegisterStartupScript(this.GetType(), "ConfirmDelete", "return confirm('Are You Sure Want To Delete?');", true);
于 2013-03-07T04:44:51.247 に答える
1

これを試して

ScriptManager.RegisterStartupScript(this, typeof(string), "ConfirmDelete", 
    "return confirm('Are You Sure Want To Delete?');", true);

編集

クライアント側のサンプルはこれを追加します

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function ShowConfirmation() {
            if (confirm("Are You Sure Want To Delete?") == true) {
                //Calling the server side code after confirmation from the user
                alert("Deleted");
            }
        }
    </script>

サーバー側の C# コード

Page.ClientScript.RegisterStartupScript(this.GetType(), "showAl", "ShowConfirmation();", true);
于 2013-03-07T05:02:13.320 に答える
1

このサンプルを使用

 string sJs = "function ConfirmDelete() { return confirm("Are you sure?"); }
      Page.ClientScript.RegisterClientScriptBlock(GetType(), "jscode", sJs, true);  
于 2013-03-07T05:02:39.867 に答える