ASP.NET Rendering usercontrols with codebehind - asp.net

I want to render a usercontrol dynamicly but my code doesn't work as expected. The codebehind won't be executed. Here is my code for rendering:
Dim ucControl As UserControl = LoadControl(pControl.VirtualPath & "/" & Control & ".ascx")
Dim ucSB As New StringBuilder
Dim ucSW As New StringWriter(ucSB)
Dim ucHTML As New HtmlTextWriter(ucSW)
ucControl.RenderControl(ucHTML)
Thank you for your help!

When rendering a UserContorl the normal lifecycle events are not called. This behavior is by design.
You could cast the UserControl to your type and call the methods explicitly:
Dim ucControl As MyUserControl = Ctype(LoadControl(pControl.VirtualPath & "/" & Control & ".ascx"), MyUserControl)
ucControl.Page_Load(me, EventArgs.Empty)

Another option is to add the dyncamically loaded control to your page as early as possible in the page lifecycle, so that the event wil be called.

Related

Event won't fire to dynamically added control

I'm dynamically adding htmlvideo controls to my web form based on the number of videos present on the server folder. This part works fine. All the videos show up and can be played. I add the 'onended' event as an attribute and the function in my code behind, but this event won't fire. I'm aware that since these controls are added after the fact I have to add a listener, but just don't know how.
This is the code that adds the controls
Dim SavePath As String = "e:\ftproot\images\TechNet\"
Dim Directory As New DirectoryInfo(SavePath)
Dim allFiles As IO.FileInfo() = Directory.GetFiles("*.mov")
Dim VidCtr As Integer = 1
For Each singlefile In allFiles
Dim myVid As New HtmlVideo
myVid.Src = "https://www.rawauto.com/images/TechNet/" & singlefile.Name
myVid.Attributes.Add("height", 140)
myVid.Attributes.Add("runat", "server")
myVid.Attributes.Add("type", "video/mp4")
myVid.Attributes.Add("controls", "controls")
myVid.Attributes.Add("onended", "VidPlayed")
myVid.Attributes.Add("id", "Vid" & VidCtr)
Panel1.Controls.Add(myVid)
Dim myLbl As New Label
myLbl.Text = Replace(UCase(singlefile.Name), ".MOV", "")
myLbl.Width = 250
myLbl.CssClass = "VidStyle"
myLbl.Font.Name = "calabri"
myLbl.Font.Bold = True
LPanel.Controls.Add(myLbl)
Next
This is the function I'm trying to fire once the user has finished watching the video:
Protected Sub VidPlayed(sender As Object, e As EventArgs)
Dim Tech As New SqlConnection("server=RAW-OTT; Initial Catalog=TechNet; Integrated Security=True;")
Dim vid As HtmlVideo = sender
Dim vidurl As String = vid.Src
VidName = Replace(vidurl, "https://www.rawauto.com/images/TechNet/", "")
If Len(VidName) > 50 Then
VidName = Mid(VidName, 1, 50)
End If
Dim SqlStr As String = "Select * From TechTube Where Video = '" & VidName & "'"
Dim ttA As New SqlDataAdapter(SqlStr, Tech)
Dim ttT As New DataTable
ttA.Fill(ttT)
If ttT.Rows.Count = 0 Then
SqlStr = "Insert Into TechTube Values ('" & VidName & "', 1, 0)"
Dim tCmd As New SqlCommand(SqlStr, Tech)
Tech.Open()
tCmd.ExecuteNonQuery()
Tech.Close()
Else
SqlStr = "Update TechTube Set Hits = Hits + 1 Where Video = '" & VidName & "'"
Dim tCmd As New SqlCommand(SqlStr, Tech)
Tech.Open()
tCmd.ExecuteNonQuery()
Tech.Close()
End If
RateLabel.Visible = True
RatingBox.Visible = True
End Sub
This is an old ViewState problem for any dynamic control, and has nothing specific to do with video.
Remember, every PostBack rebuilds the entire page from scratch, including your dynamic controls. If you still want to see these controls after a postback (which includes all server events), you must re-add them to the page. Additionally, you need a control's ViewState restored if you want an event fired for the control during this PostBack, and for the ViewState to restore the control must be added back to the reconstructed page before the Page_Load event runs. Page_Init or Page_PreInit can work well for this.
Finally, consider the performance implications here. Do you really want to rebuild the entire page on every user interaction, or is it perhaps time to learn to use javascript to process these things, with maybe a web api that only has to receive a javascript request without causing an entire page cycle both on your server and in the user's browser?
Just a few of the many other times this has been asked and answered:
Dynamic Event Handler not Firing
dynamically added buttons not firing click event c#
dynamically created button click event not firing
Dynamically Added DropDownlists Are Not Firing SelectedIndexChanged Event
ASP.NET: Viewstate and programmatically adding user controls
Click events on Array of buttons
VB ASP dynamic button click event not hitting handler event

Can't clear credentials textboxes on ASP.NET CreateUserWizard Control

I have a CreateUserWizard control using forms authentication on my login/create user page. I customized the CreateUserWizardStep1 so that I could add some additional validators.
After successfully creating a user with the control and it displays "Complete
Your account has been successfully created." I have added an additional button that will allow the person to create another user by setting the ActiveStepIndex = 0. The problem is, while it sets the ActiveStepIndex correctly, it retains the old user account credentials. I try to clear them manually using the following code, but they still stick...Anyone have any ideas?
Protected Sub btnCreateAnotherUser_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.cuwMain.ActiveStepIndex = 0
CleanCreateNewUserInput()
End Sub
Private Sub CleanCreateNewUserInput()
Dim txtUserName As TextBox
txtUserName = FindControlIterative(Me.cuwMain, "UserName")
txtUserName.Text = String.Empty
Dim txtPassword As TextBox
txtPassword = FindControlIterative(Me.cuwMain, "Password")
txtPassword.Text = String.Empty
Dim txtConfirmPassword As TextBox
txtConfirmPassword = FindControlIterative(Me.cuwMain, "ConfirmPassword")
txtConfirmPassword.Text = String.Empty
Dim txtEmail As TextBox
txtEmail = FindControlIterative(Me.cuwMain, "Email")
txtEmail.Text = String.Empty
Dim txtQuestion As TextBox
txtQuestion = FindControlIterative(Me.cuwMain, "Question")
txtQuestion.Text = String.Empty
Dim txtAnswer As TextBox
txtAnswer = FindControlIterative(Me.cuwMain, "Answer")
txtAnswer.Text = String.Empty
End Sub
It finds the textboxes correctly, but it does not actually reset their values, even though in the debugger it says it did.
Thoughts ?
What happens if you call Response.Redirect(Request.Url.ToString(), true)? That should clear everything for you.
Also, the recursive nature of the FindControlIterative call would make your code quite expensive to run as it has to drill down into the control heirarchy for every control that you are looking for.
The problem with your code is that:
In a Wizard control, ViewState is not responsible for storing the modified values for controls such as TextBoxes. These controls implement the IPostBackDataHandler interface. The LoadPostBackData event fires in the page lifecycle, in which the VALUES of the controls load from the form HTTP POST headers... which are resubmitted by the client...
So how to destroy the HTTP POST Headers to clear the control values?
A new request results in new HTTP POST Headers... simply do this in the Button click event handler:
Response.Redirect(Page.Request.Url.ToString());
This has the added benefit that it goes to Step 1 of the wizard so you also dont have to do... wiz.MoveTo(wiz.WizardSteps[0]).
Credit to Konrad - ASP.Net Wizard - How to clear contents of web controls
I feel silly posting this..., but I just turned viewstate off on the CreateUserWizard control and that did it.
Thanks for the help Daniel, I now have a better understanding on how ASP.NET stores information.

ASP.NET Dynamic User Controls

I'm sure this question has been asked a million times, however I haven't been able to find an answer that solves my problem.
I am programmatically adding some custom user controls to a PlaceHolder which I have on a simple aspx page. All of the user controls Postback's work correctly except for one which has a Gridview on it.
For some reason any postback that gets fired from within this control, does not call the specified event on the first click, however all future clicks it will work fine. I have no idea why this is the case, but many solutions I have found, suggest adding an ID to the ascx User Control, however this doesn't work in my case.
I've taken a look at the source file for the page that gets generated before and after the first click, javascript used for calling the postback changes, i.e
Before first click: onclick="javascript:__doPostBack('tmpControlID$sgvPrimaryEmploymentHistory','Select$0')"
After first click: onclick="javascript:__doPostBack('OFFHome1$tmpControlID$sgvPrimaryEmploymentHistory','Select$0')"
OFFHome1 is the parent user control which exists on the aspx page. All other controls are added to a placeholder in this control, i.e.
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="OFFHome.ascx.vb" Inherits="UmbracoUserControls.OFFHome" %>
<asp:PlaceHolder ID="phOFFSection" runat="server"></asp:PlaceHolder>
Nothing to complicated. Then in the code behind the controls are loaded into the placeholder using the following:
Private Sub LoadNextOFFStep()
Dim ControlName As String = "TestControl.ascx"
phOFFSection.Controls.Clear()
If ControlName IsNot Nothing AndAlso ControlName <> String.Empty Then
Dim NewControl As Object = LoadControl(ControlName)
With NewControl
.ID = USERCONTROLNAME
Dim StepCompleted As Boolean = .FillControl()
If StepCompleted Then
Exit Sub
End If
Dim AllowSkip As Boolean = .AllowSkip()
btnSkip.Visible = AllowSkip
End With
phOFFSection.Controls.Add(NewControl)
End If
End Sub
Again, nothing overly complicated. The USERCONTROLNAME is just a const with the value "tmpControlID" in it.
The control that is giving me trouble is a little complicated, I was originally using a custom GridView control that we have created, but have removed it and replaced it with the standard asp one to see if the problem still occurs, and it does.
Any button, on control which fires off a postback will fail the first time, and all future click will work correctly. On the first click the Page_Load event will get called, but that is it.
What am I doing wrong??
After far too much time spent on this, I have finally worked it out.
It was to do with the order of events, however just not where I had thought. The FillControl function was getting called before User Control had been added to the PlaceHolder. I changed this so that it gets called after the User Control was added to the PlaceHolder and now it works first time.
Basically the code looks like this now:
Private Sub LoadNextOFFStep()
Dim ControlName As String = "TestControl.ascx"
phOFFSection.Controls.Clear()
If ControlName IsNot Nothing AndAlso ControlName <> String.Empty Then
Dim NewControl As Object = LoadControl(ControlName)
With NewControl
.ID = USERCONTROLNAME
Dim AllowSkip As Boolean = .AllowSkip()
btnSkip.Visible = AllowSkip
End With
phOFFSection.Controls.Add(NewControl)
Dim StepCompleted As Boolean = CType(phOFFSection.Controls(0), Object).FillControl()
If StepCompleted Then
LoadNextOFFStep()
Exit Sub
End If
End If
End Sub
Thanks for everyone's help.
Well... the answer to the first question is simple: It fails the first time because the ID of the control (or rather, in the postback script) is different on subsequent page loads. It works on subsequent clicks because the control ID stays the same.
Now as to WHY that is... much tougher! But probably something to do with the order of operations here.
Try explicitly setting the NamingContainer value for NewControl:
With NewControl
.NamingContainer = OffHomeOne; // whatever
.ID = USERCONTROLNAME

Problem while Creating Dynamic ModalPopup

i want to create a modalpopup dynamically but i come across a problem.I pasted my sub here and i dont know what to do for that problem.When i want to show modalpopup,it says
"Control 'mdldelete2' of type 'ModalPopupExtender' must be placed inside a form tag with runat=server." How can i solve this?
Public Sub Raise_Alarm(ByRef p_Page As Page,
ByRef p_AssignedButton As System.Web.UI.WebControls.Button,
ByVal p_Message As String)
Dim mdldelete2 As Global.AjaxControlToolkit.ModalPopupExtender =
p_Page.Page.FindControl("mdldelete2")
If mdldelete2 Is Nothing Then
mdldelete2 = New Global.AjaxControlToolkit.ModalPopupExtender
End If
With mdldelete2
.TargetControlID = p_AssignedButton.ID
.PopupControlID = "pnlDelete"
.ID = "mdldelete2"
.BackgroundCssClass = "modalBackground"
.OkControlID = "btnDeleteOk"
.CancelControlID = "btnDeleteCancel"
End With
p_Page.Controls.Add(mdldelete2)
Dim mylabel As Label
mylabel = p_Page.FindControl("lblStatus")
mylabel.Text = p_Message
mdldelete2.Show()
End Sub
Really, you should be adding the mdldelete2 control to the Controls collection of the Form control, rather than the Page directly - that might help.
I often find it's easier to add a PlaceHolder control to the page for this sort of thing - it doesn't add anything directly to the page, but gives you a named container to find and add controls to.
Also, just a point - if you did find an instance of the control with the Page.FindControl method, then you don't need to add it to a form collection again, as it's already in there.
Looks like you need to add a ScriptManager control to the aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

Find controls on sharepoint master page

I'm trying to loop through all the controls on a sharepoint page, for the purposes of testing i just want to output the control ID
this is the code i'm using
Public Shared Sub SubstituteValues3(ByVal CurrentPage As Page, ByRef s As StringBuilder)
'Page()
'- MasterPage
'- HtmlForm
'- ContentPlaceHolder
'- The TextBoxes, etc.
For Each ctlMaster As Control In CurrentPage.Controls
If TypeOf ctlMaster Is MasterPage Then
HttpContext.Current.Response.Output.Write("Master Page <br/>")
For Each ctlForm As Control In ctlMaster.Controls
If TypeOf ctlForm Is HtmlForm Then
HttpContext.Current.Response.Output.Write("HTML Form <br/>")
For Each ctlContent As Control In ctlForm.Controls
If TypeOf ctlContent Is ContentPlaceHolder Then
HttpContext.Current.Response.Output.Write("Content Placeholder <br/>")
For Each ctlChild As Control In ctlContent.Controls
HttpContext.Current.Response.Output.Write(ctlChild.ID.ToString & "<br />")
Next
End If
Next
End If
Next
End If
Next
HttpContext.Current.Response.Output.Write("--------------")
HttpContext.Current.Response.End()
however it's not getting past the 'MasterPage' output.
I would expect to see the names of all the controls i have inside my content placeholder but i find it all a bit confusing.
Start with Page.Master.Controls
From there what you have should basically work
For Each ctlForm As Control In Page.Master.Controls
If TypeOf ctlForm Is HtmlForm Then
HttpContext.Current.Response.Output.Write("HTML Form <br/>")
For Each ctlContent As Control In ctlForm.Controls
If TypeOf ctlContent Is ContentPlaceHolder Then
HttpContext.Current.Response.Output.Write("Content Placeholder <br/>")
For Each ctlChild As Control In ctlContent.Controls
HttpContext.Current.Response.Output.Write(ctlChild.ID.ToString & "<br />")
Next
End If
Next
End If
Next
A MasterPage isn't a control of the current page, it's a property of it, in Page.MasterPage
i found this piece of code which seems list the controls I need, i think it's more of a hack though.
For i = 0 To CurrentPage.Request.Form.AllKeys.Length - 1
If CurrentPage.Request.Form.GetKey(i).Contains("ctl00$PlaceHolderMain$") Then
Dim key As String = CurrentPage.Request.Form.GetKey(i).Substring(22)
Dim keyText As String = String.Format("[{0}]", key)
HttpContext.Current.Response.Output.Write(keyText & "<br/>")
'Text.Replace(keyText, CurrentPage.Request.Form("ctl00$PlaceHolderMain$" & key))
End If
Next
you can do this simply with recursion, not efficent, but it is simple... try this method:
public void getControls(Control input)
{
foreach (Control c in input.Controls)
{
Response.Write(c.GetType().ToString() + " - " + c.ID + "<br />");
getControls(c);
}
}
And call it like this:
getControls(Page);
That will cycle trough all controls on your page and output the type - ID of them and print it out to the top of the page... you could also use the code to construct a list or whatever you want to do.

Resources