Friday, April 15, 2011

How can I call a function in code behind file in ASP.NET?

I have ShowValue working. Now, on the last line I need to call the function newFunction to repopulate the dropdownlist object on the onClick event.

I am getting errors when I click the dropdownlist down arrow.

How do I get the two to hand shake with each other?

My code:

Public Sub ShowValue(ByVal sender As Object, ByVal e As System.EventArgs)

    lblupdatePanel.Text = DropDownList1.SelectedValue.ToString
    Dim LocationDescription2 As DropDownList = CType(dvContact.FindControl("LocationDescription2"), DropDownList)

    Dim LocationLogic As New LocationBLL
    LocationDescription2.DataSource = LocationLogic.GetUnitByUnitID(DropDownList1.SelectedValue.ToString)
    LocationDescription2.DataTextField = "LocationDescription"
    LocationDescription2.DataValueField = "LocationCode"
    LocationDescription2.SelectedValue = DropDownList1.SelectedValue.ToString
    LocationDescription2.DataBind()

    LocationDescription2.Attributes.Add("onclick", "newFunction(what to put here?);")

End Sub

Public Sub newFunction(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim LocationDescription2 As DropDownList = CType(dvContact.FindControl("LocationDescription2"), DropDownList)
    Dim DivisionDescription1 As DropDownList = CType(dvContact.FindControl("DivisionDescription1"), DropDownList)

    Dim LocationLogic As New LocationBLL
    LocationDescription2.DataSource = LocationLogic.GetLocationByDivisionCode(DivisionDescription1.SelectedValue.ToString)
    LocationDescription2.DataTextField = "LocationDescription"
    LocationDescription2.DataValueField = "LocationCode"
    LocationDescription2.DataBind()

End Sub
From stackoverflow
  • LocationDescription2.Attributes.Add("onclick", "newFunction(what to put here?);"

    Adds a client side javascript event handler which can't call VB.NET codebehind functions. You need to wire the event up in VB.NET

    So it will trigger a postback at which time ASP.NET can invoke the codebehind event handler.

    Your best off defining event handlers in Page_Load though.

    Joel Coehoorn : And except that there is no click event for that control.
    Joel Coehoorn : Also: he's using VB.Net
    Gail : I am following you, Location....("onclick")... adds a client side javascript which will not call Public sub newFunction....? So add LocationDescription2.Click += newFunction; which raised an error. 2) Would not attributes.add("onclick")... force a Click event for that control?
    Martijn Laarman : Here's for assumptions :) VB.NET was implied but edited to be more explicit.
    Martijn Laarman : Gail See Joel Coehoorn's answer which explains how and what event to hook on the server side in VB.NET.
  • The Attributes.Add method you're calling only effects the rendered html for the control. The rendered HTML can only directly call javascript functions. You need this code to be called at the server. Remember to keep the HTML/DOM view of a page and server view of a page separate in your head.

    To hook up an event in the server view, the vb.net syntax looks like this:

    AddHandler LocationDescription2.[EventName], AddressOf newFunction
    

    Unfortunately, the DropDownList control does not have a click event, so you will need to replace EventName from my sample with one of the events shown here:
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist_events.aspx

    You'll have to settle for the events included in that last, or do some complicated javascript work to simulate a Click event.

    Additionally, it's important to remember that when you handle server side events not only does your specific event handler run, but the entire page is rebuilt as well. It does a full postback (ajax aside for the moment). You need to make sure that's really what you intend.

    Gail : ...newFunction raised an error. Originally had the page working with cascading dropdownlist. User wants something that is, at this time, beyond me. The search for a solution began here, http://forums.asp.net/p/1399324/3023937.aspx#3023937, and I think there is no solution. Thank you. Gail
  • ASP.Net Ajax Toolkit. Look for the Cascading Dropdown Lists and the associated examples. It'll look at act better too.

    ASP.Net Ajax Toolkitt

0 comments:

Post a Comment