0

リンクボタンが押されたときに、いくつかのjavascriptといくつかのコードビハインド(私にとってはC#)を呼び出すフォームがあります。通常の操作は次のように動作します。リンク ボタンをクリックしてフォームのラベルからテキストを取得し、HTML エディターを開き、テキスト エディターで [完了] をクリックしてテキストを保存し、フォームにテキストを再度表示します。それはすべて実行されますが、エラー (コントロールが定義されていません) が発生し、フォームで他の作業を行うことができなくなります。

まず、HTML エディターと HTML エディターを保持するウィンドウである aspx コードを次に示します。

<asp:Panel ID="EditCommentPopup" runat="server" SkinID="PopupPanel" HorizontalAlign="Center" Width="500px" Style="display:none;">
                <div id="SendCommentHeader" class="modalPopupHeader">
                    <table width="100%" style="vertical-align: middle">
                        <tr>
                            <td style="width: 50%; text-align: left; padding-left: 5px;">
                                <asp:Label ID="EditCommentHeaderLBL" runat="server" Font-Bold="true" Text="Change Comment" ></asp:Label>
                            </td>
                            <td style="width: 50%; text-align: right; padding-right: 3px;">
                                <asp:Button SkinID="SubmitButton" ID="SaveComment" runat="server" Text="Done" OnClientClick="submitEditorPopup(); return true;" OnClick="SubmitCommentButton_Click"/>
                                <asp:Button SkinID="CancelButton" ID="cancelCommentButton" runat="server" Text="Cancel"/>
                            </td>
                        </tr>
                    </table>
                </div>
                <ajaxToolkit:ModalPopupExtender ID="EditCommentPopupExtender" runat="server" TargetControlID="EditCommentHeaderLBL"
                        PopupControlID="EditCommentPopup" BackgroundCssClass="modalBackground" PopupDragHandleControlID="EditCommentPopup"
                        DropShadow="False" CancelControlID="cancelCommentButton" BehaviorID="ChangeCommentsPopup" >
                </ajaxToolkit:ModalPopupExtender>
                <asp:Panel ID="EditCommentInnerPanel" SkinID="PopupInnerPanel" HorizontalAlign="Center" runat="server" >
                    <asp:Label ID="CommandArgLBL" runat="server" Visible="false"></asp:Label>
                    <custom:CustomTextEditor ID="EditCommentsTextBox" runat="server" Width="100%" Height="300px" NoScript="true" />
                </asp:Panel>
            </asp:Panel>

ここに私のJavaScript関数があります

var contentLBL;
var editorControl;
var hiddenField;
function LoadEditorPopup(labelID, editorID, hiddenID, linkID) {
    editorControl = document.getElementById(editorID);
    contentLBL = document.getElementById(labelID);
    hiddenField = document.getElementById(hiddenID);
    editorControl.control.set_content(contentLBL.innerHTML);
    var popup = $find("ChangeCommentsPopup");
    popup.show();

}

function submitEditorPopup() {
    var tmp = editorControl.control.get_content(); //error flags here
    contentLBL.innerHTML = tmp;
    var str= new String();
    str = tmp;
    str = str.replace(/</g, "{%");
    str = str.replace(/>/g, "%}"); //stripping out html tags to prevent sql insertion
    hiddenField.value = str;
    var popup = $find("ChangeCommentsPopup");
    popup.hide();
}

そして、ここに私のC#関数があります

protected void ShowEditCommentPopup(object sender, EventArgs e)
{
    string arg = ((LinkButton)sender).CommandArgument;
    string content = "";
    string title = "";
    switch (arg)
    {
        //doing stuff, setting content and title
    }
    EditCommentHeaderLBL.Text = title;
    CommandArgLBL.Text = arg;
    EditCommentsTextBox.Content = content;
    EditCommentPopupExtender.Show();

    EditModalPopupExtender.Show(); //this is the form
}

ここで何が起きているかというと、linkbutton (aspx コードには表示されていません) をクリックすると、javascript の LoadEditorPopup 関数が起動します。それはその仕事をし、コードビハインドの次の関数ShowEditCommentPopupが呼び出されるように、onclientclick呼び出しで「trueを返す」があります。エディターがポップアップし、問題なく使用できます。[完了] をクリックすると、JavaScript で SubmitEditorPopup イベントが発生し、変数 EditorControl が未定義であるというエラーが表示されます。ShowEditCommentPopup メソッドを呼び出さない場合、エラーは発生しないことがわかりましたが、最初に抱えていた別の問題に戻ります。この問題が修正されれば修正されることを願っています。

それが十分に明確であることを本当に願っています。前もって感謝します!

4

1 に答える 1

0

JavaScript 関数を含むスニペットがあります。そのコードはどこにありますか? 2 度目に呼び出される可能性のあるクラスまたは関数の内部ですか? その場合は、var editorControl;再度実行して、以前の editorControl 変数をオーバーライドすることができます。確認するために、すぐにアラートを出しvar editorControl;ます。

alert( 'Should only happen once' );
var editorControl;

それは役に立たなかったので、交換してみてください。

var tmp = editorControl.control.get_content();

tmp = document.getElementById(editorID);

submitEditorPopup で。

于 2012-04-11T16:36:09.820 に答える