Dynamic UC problems

  • Thread starter Thread starter David
  • Start date Start date
D

David

Hi,

Further to my earlier post about using dynamic usercontrols (which I have
still yet to solve), I have another problem.


I am creating the UC in the page by LoadControl. The page itself has a
submit button that is inside an AJAX UpdatePanel.

When I submit the page, the contents of the text boxes of the UC don't
appear to be there.


I have made a public sub in my UC. I pass an ID to the sub. This happens
when the submit button on the page is clicked and it works fine (to this
point).

sort of like this...

Page.
button1_click
begin
TravelUC.SaveTravel(ID)
end


UC
public sub SaveTravel(ID as integer)
begin
code to save the details.
end

However, when I am in the SaveTravel sub routine, the values of my text
boxes (or drop down box) are seemingly empty. Why would this be? Any ideas
on how I can fix it?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
Found the problem...

Here is my code...

(my Page)
Partial Class _Default Inherits System.Web.UI.Page

Dim TravelUC = LoadControl(ResolveUrl("~/UserControls/travel.ascx"))

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If Not IsPostBack Then

LeftPanel.Controls.AddAt(LeftPanel.Controls.IndexOf(Panel5),
TravelUC)

End If

End Sub



Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim AttendeeID as integer = 1

TravelUC.SaveTravel(AttendeeID)

End Sub



(My UC)

Public Sub SaveTravel(ByVal RegistrationID As Integer)

Dim conn As New SqlConnection(connStr)

Dim cmd As New SqlCommand("usp_AttendeeTravel")

cmd.Parameters.Add("@RegID", SqlDbType.Int).Value = RegistrationID

cmd.Parameters.Add("@TravelBy", SqlDbType.VarChar).Value =
TravelDDL.SelectedValue

cmd.Parameters.Add("@PassportName", SqlDbType.VarChar).Value =
PassportName.Text

cmd.Parameters.Add("@HomeAddress1", SqlDbType.VarChar).Value =
HomeAddress.Text

cmd.Parameters.Add("@HomeAddress2", SqlDbType.VarChar).Value =
HomeAddress2.Text

cmd.Parameters.Add("@HomeAddress3", SqlDbType.VarChar).Value =
HomeAddress3.Text

Try

conn.Open()

funcDB.ExecNonQuery(cmd)

cmd.Dispose()

Catch ex As Exception

Finally

conn.Close()

End Try

End Sub


What I didn't know is that the ajax call runs the page load on EVERY ajax
call. As my TravelUC was inside a Not IsPostBack, it wasn't being
re-loaded, even though it remained on the page (due to ajax not refreshing
the whole page).

Moved it outside of the Not IsPostBack and it works.


Still got my other prob though...

I want to be able to dynamically load any user control without knowing
beforehand (as in the above code) what the UC is. i.e. I can have a list of
user controls but I only want to load whichever specific control by
attributes known to the page. I don't want to LoadControl all the controls
on the offchance that I might use it.

The problem as you can see is that my LoadControl happens outside of any
function so that the UC is available to any other functions. I would have
put the loadcontrol inside the pageload, but then I can't call upon any UC
functions from elsewhere in my page.

Any ideas?


Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
Back
Top