2

複数行のテキスト ボックスの最大長を設定しようとしています。私が遭遇している障害は、ページの読み込み時に JavaScript で設定できないことです。これは、InsertItemTemplate にあるためです。ページの読み込み時に、レコードを追加して InsertItemTemplate が表示されるまで、javascript エラー (エラー: 未定義または null 参照のプロパティ 'maxLength' を設定できません) がスローされます。

        <html xmlns="http://www.w3.org/1999/xhtml">
        <head runat="server">
        <script type="text/javascript">
        function onPageLoad() {
            var txtAddNotes = document.getElementById("formViewNewItem_NotesTextBox");
            txtAddNotes.maxLength = 200;
         }
        </script>
        </head>
            <body onload="onPageLoad()">
       <asp:FormView ID="formViewNewItem" runat="server" DataKeyNames="ID" 
        DataSourceID="datasource1" OnDataBound="formViewNewItem_DataBound">
            <InsertItemTemplate>
            <asp:TextBox ID="NotesTextBox" runat="server" Text='<%# Bind("Notes") %>' TextMode="MultiLine" Rows="3" Width="350px" />
            </InsertItemTemplate>
       </asp:FormView>
    </body>
    </html>
4

1 に答える 1

1

formviewDefaultMode="Insert"を変更してみてください。そうすれば、スクリプトはページの読み込み時にテキストボックスを見つけることができるはずです。お役に立てれば。

編集
ページの読み込み時にフォームビューの現在のモードを確認することで、js 関数を呼び出すことができます。

    protected void Page_Load(object sender, EventArgs e)
    {
        if(formViewNewItem.CurrentMode == FormViewMode.Insert)
        {
             ClientScript.RegisterStartupScript(this.GetType(), "PageLoad", "onPageLoad();", true);
        }
    }
于 2013-05-26T00:27:29.190 に答える