onblur in an UpdatePanel

  • Thread starter Thread starter smay
  • Start date Start date
S

smay

I'm having trouble with a datagrid in an UpdatePanel that includes a user
control in the footer and edit rows. The user control references a js file
that initializes an onblur for the textbox it contains. This is fine when
the page loads however after clicking the add button, the update panel
refreshes showing me the row I added and a new footer, but the code contained
in the onblur function has been added to the page's javascript as if it were
a validator. The problem is that at this point it can no longer reference
the appropriate objects. In other words, near the bottom of my code snippets
below, "doStuff(oThis.textbox);" is being placed into the anonymous
javascript function at the bottom, which then throws an error because oThis
is undefined. Can anyone tell me why this is happening and how to avoid it?
Any I doing something wrong that is causing this?

Parent Page HTML:
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:DataGrid ID="Components" runat="server" ...>
<Columns>
<asp:TemplateColumn>
<FooterTemplate>
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</ContentTemplate>
</asp:UpdatePanel>

Parent Page CodeBehind:
Dim excip As New myUC
Protected Sub Page_Init
initUC()
End Sub

Private Sub initUC()
excip = CType(LoadControl("~/myUserControl.ascx"), myUC)
End Sub

Private Sub Page_Load
BuildDataGrid()
End Sub

Protected Sub Components_ItemDataBound
Select Case e.Item.ItemType
Case ListItemType.Footer
Dim ph As PlaceHolder = CType(e.Item.FindControl("ph"), PlaceHolder)
excip.Property1 = ""
excip.Property2 = ""
ph.Controls.Add(excip)
End Sub

Protected Sub Components_ItemCommand
'save info

initCBO()
BuildDataGrid()
End Sub


User Control HTML:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>

User Control CodeBehind:
Private Sub Page_Load
If (Not Page.ClientScript.IsClientScriptIncludeRegistered(Me.GetType,
"jsRef")) Then
Page.ClientScript.RegisterClientScriptInclude(Me.GetType, "jsRef",
ResolveClientUrl("~/myUserControl.js"))

Dim sb as New StringBuilder
sb.Append("dynamic javascript")
ScriptManager.RegisterStartupScript(Literal1, Me.GetType(), "dynamicJS",
sb.ToString, False)
End If
End Sub


myUserControl.js:
function myStuff(txtbox) {
this.textbox = txtbox;

this.init();
}

myStuff.prototype.init = function () {
var oThis = this;

this.textbox.onblur = function (event) {
doStuff(oThis.textbox);
};
};


Produced validation code:
function anonymous() {
ValidatedControlOnBlur(event);
doStuff(oThis.textbox);
}
 
Back
Top