Reputation: 1233
I'm trying to set the maxlength of a multi-line textbox. The snag I'm running into is I can't set it with javascript on page load because it is in an InsertItemTemplate. On page load it throws a javascript error (Error: Unable to set property 'maxLength' of undefined or null reference) until I go to add a record and the InsertItemTemplate shows up.
<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>
Upvotes: 2
Views: 2134
Reputation: 6851
Try changing the formview DefaultMode="Insert"
, then your script should be able to find the textbox on page load. Hope this helps.
EDIT
You can try calling your js function by checking for the current mode of the formview on page load:
protected void Page_Load(object sender, EventArgs e)
{
if(formViewNewItem.CurrentMode == FormViewMode.Insert)
{
ClientScript.RegisterStartupScript(this.GetType(), "PageLoad", "onPageLoad();", true);
}
}
Upvotes: 1