Category Archives: DynamicControl

[RESOLVED] Simulate Hyperlink clicked

I would like to create a dynamic Hyperlink (I know how to to that), and then simulate it's clicked event when some other event has taken place.

Any ideas?

Help most appreciated.

Hi,

Do you mean Event handler?

DynamicLinkButton.Click += new System.EventHandler(this.myEventHandler);

OR

http://msdn.microsoft.com/en-us/library/t3d01ft1.aspx

To get hyperlink Click event you got to use some javascript - 

http://programming.top54u.com/post/ASP-Net-HyperLink-OnClick-Event.aspx

And to dyncamically create hyperlinks - 

http://forums.asp.net/t/1146597.aspx

PS - Add OnClick to the attributes list of the dynamically created hyperlink....i mean the javascript to the onclick...

Thanks.

Here's a JS tweak to acheive that

 

//aspx
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptManager" runat="server" />
    <script language="javascript" type="text/javascript">
        function xyz(a1) {
            alert(a1);
        }

    </script>
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Simulate HyperLink EventHandler" />
    </div>
    
    </form>
</body>

//codebehind
public partial class DynamicControls : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateHyperLink();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HyperLink hl = (HyperLink)this.FindControl("h1");
        ClientScriptManager script = Page.ClientScript;

        if (!script.IsClientScriptBlockRegistered(this.GetType(), "Alert"))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Alert", "xyz('"+hl.NavigateUrl+"');", true);
        }
    }

    private void CreateHyperLink()
    {
        HyperLink hl = new HyperLink();
        hl.ID = "h1";
        hl.Attributes.Add("runat", "server");
        hl.Text = "Dynamic HyperLink";
        hl.Target = "_blank";
        hl.NavigateUrl = "~/CallUC.aspx";
        this.form1.Controls.Add(hl);
    }
}

[RESOLVED] Extender controls may not be registered after PreRender.

I am creating Dynamic UserControls with Dynamic AjaxControls. Then I store each of the UserControls in a Collection. On Postback I recall the UserControls in the collection to Re-Create the interface. It works if i only use Standard HTML controls but does NOT work with AJAX controls. I get this error on PostBack Extender controls may not be registered after PreRender. So far I have tried the following:

  1. MyBase.OnPreRender(e) in Page and Usercontrol
  2. EnsureChildControls() in the OnInit
  3. Added ScriptManager to the MasterPage None of this stuff is working...

If I re-create all the controls including the ones inside the usercontrol then i get no error however it takes forever to do a postback as opposed to just recalling the collection of UserControls.

Dynamically adding user control is always pretty tricky.  Are you using the CreateChildControls method to add your controls to the page or are you just doing it in PreRender?

http://msdn.microsoft.com/en-us/library/system.web.ui.control.createchildcontrols.aspx

mkubaryc

Dynamically adding user control is always pretty tricky.  Are you using the CreateChildControls method to add your controls to the page or are you just doing it in PreRender?

http://msdn.microsoft.com/en-us/library/system.web.ui.control.createchildcontrols.aspx

I tried it on the OnInit, Page_Load, and CreateChildControls. If i dont use Ajax controls in the User controls it works fine, but as soon as I add in the Ajax Extenders or other ajax controls I get the PreRender Error.

 

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    MyBase.OnInit(e)
    If IsPostBack Then
        Call ReCreateControls()
    Else
        dynamicControls.Clear()
        Call CreateControls()
    End If

    EnsureChildControls()
End Sub

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
    MyBase.OnPreRender(e)
End Sub

'Protected Overrides Sub CreateChildControls()
'    MyBase.CreateChildControls()
'    If IsPostBack Then
'        Call ReCreateControls()
'    Else
'        Call CreateControls()
'    End If
'End Sub

OnInit is way too early, Viewstate isn't ready and the statebag hasn't been loaded. Page_Load may be too late. There is actually an event called OnLoad, which occurs before Page_Load and that may be the place to do it. Sometimes though, if you change things you have to create them in both onLoad and PreRender. The first catches the postback event from the original control, then the prerender changes it to use the new one. For example, clicking a button in a usercontrol that changes the control. Page must create that first control in order to handle the button postback, then removes the first usercontrol and replaces it with the second usercontrol. in the PreRender.

markfitzme

OnInit is way too early, Viewstate isn't ready and the statebag hasn't been loaded.

So do i only do this in the Page that hosts the UserControls? Here is what i did in the main page but no avail...

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

MyBase.OnLoad(e) If IsPostBack Then Call ReCreateControls() Else dynamicControls.Clear() Call CreateControls() End If EnsureChildControls() End Sub Protected Overrides Sub OnInit(ByVal e As System.EventArgs) MyBase.OnInit(e) End Sub Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs) MyBase.OnPreRender(e) If IsPostBack Then Call ReCreateControls() End If End Sub

The ReCreateControls method just adds the populated UserControls from collection back into the PlaceHolder.

Private Sub ReCreateControls()
    If Not DesignMode Then
        Dim ph As PlaceHolder = hp.FindControl("HotPanelPlaceHolder")
      
        For Each c As MyControl In dynamicControls
              ph.Controls.Add(c)
        Next

    End If
End Sub

Am I supposed to add any code in the PreRender of UserControl or OnLoad?

Hi,

simple_man

So do i only do this in the Page that hosts the UserControls? Here is what i did in the main page but no avail...

For my experience ,you can create AjaxControls dynamically in Page_init event.

simple_man

I am creating Dynamic UserControls with Dynamic AjaxControls. Then I store each of the UserControls in a Collection. On Postback I recall the UserControls in the collection to Re-Create the interface. It works if i only use Standard HTML controls but does NOT work with AJAX controls.

Actually ,here is a wonderful thread which describes how to create Dynamic UserControls with Dynamic AjaxControls.

Please check it and see whether it helps.

simple_man

I tried it on the OnInit, Page_Load, and CreateChildControls. If i dont use Ajax controls in the User controls it works fine, but as soon as I add in the Ajax Extenders or other ajax controls I get the PreRender Error.

Since the issue is related to ASP.NET Ajax and the Ajax Control Toolkit, please post the thread at Ajax forums ,there are more ajax experts in that forums.

Hope this helps.

[RESOLVED] .NET bug in .FindControl() functionality; killing events for dynamic Repeater controls. Test case inluded.

I have already tried posting this bug, but for some reason it wasn't accepted by the moderators... The issue here is using the .FindControl() function (ex: Page.FindControl([ControlID])) with the UniqueID of dynamic controls coming from RepeaterItems. Doing so removes the internal reference to the event of the control. In other words, if a TextBox (ID=txtInput, UniqueID=repeater$ctl00$txtInput, AutoPostBack=True), which has its TextChanged event bound to txtInput_TextChanged, causes a postBack, and on Page_Init Page.FindControl('repeater$ctl00$txtInput') is called - the txtInput_TextChanged event will not be called. If the Page.FindControl() function, however, is not called - everything functions as expected. Here's a direct link to a test case that I created, uploaded to my personal website, as well as all the source code:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="PageFindControlTestCase.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<asp:TextBox ID="txtOutside" runat="server" AutoPostBack="true" OnTextChanged="TextChanged"></asp:TextBox>

		<asp:Repeater ID="repGrid" runat="server">
			<ItemTemplate>
				<asp:TextBox ID="txtInside" runat="server" AutoPostBack="true" OnTextChanged="TextChanged"></asp:TextBox>
			</ItemTemplate>
		</asp:Repeater>
    </div>
    </form>
</body>
</html>
Public Class WebForm1
	Inherits System.Web.UI.Page

	Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
		If Not IsPostBack Then
			Dim Table As New DataTable()
			Table.Columns.Add("Column1")
			Dim Row As DataRow = Table.NewRow()
			Row("Column1") = ""
			Table.Rows.Add(Row)

			repGrid.DataSource = Table
			repGrid.DataBind()
		End If

		Dim EVENTTARGET As String = Page.Request.Params("__EVENTTARGET")
		If EVENTTARGET IsNot Nothing AndAlso EVENTTARGET <> String.Empty Then
			' This line kills TextChanged event and repeater when page postbacks from txtInside. '
			' Executing Page.FindControl("repGrid$") when postback comes from txtOutside kills the repeater as well. '
			Page.FindControl(EVENTTARGET)
		End If
	End Sub

	Protected Sub TextChanged(sender As Object, e As EventArgs)
	End Sub

End Class

please see

http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Can you explain why you pointed me to that page? I know that control well and have used it before, but cannot understand how it would be helpful in my situation.

Perhaps somebody from MSFT would like to pitch in an idea on what's happening?

[RESOLVED] Creating a dynamic click event for linkbutton (urgent)

this is driving me crazy and I am sure the answer is simple but it is one of those days that I can't see it. I just can't see to add click event to a dynamicalled created linkbutton

here is my code



Private WithEvents link As LinkButton
 Private Function CreateLinkButton(ByVal text As String) As LinkButton
        link = New LinkButton()
        link.PostBackUrl = lbl_link_url.text & text
        link.Text = text
        link.Font.Size = 10
        link.Font.Bold = True
        link.Font.Underline = True
        link.ID = "link" & text
        AddHandler link.Click, AddressOf Click_Initilize


        Return link
       
    End Function


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

        Initilize()

    End Sub

 

 

I also tried this but still no luck

 

  Private Function CreateLinkButton(ByVal text As String) As LinkButton
        link = New LinkButton()
        link.PostBackUrl = lbl_link_url.text & text
        link.Text = text
        link.Font.Size = 10
        link.Font.Bold = True
        link.Font.Underline = True
        link.ID = "link" & text
        AddHandler link.Click, AddressOf Click_Initilize


        Return link
       
    End Function


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

        Initilize()

    End Sub


    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim control As Control
        For Each control In phLinks.Controls
            If TypeOf control Is LinkButton Then
                link = CType(control, LinkButton)

                AddHandler link.Click, AddressOf Click_Initilize

            End If

        Next
    End Sub

 

ur help will be appreciated

Try this:

link.Click += New EventHandler(AddressOf Click_Initilize)

this should make you confortable with dynamic controls....

http://blog.krisvandermast.com/AddingADynamicControlToAPlaceholderControlAndWireUpTheEvent.aspx

Thanks.

link.Click += New EventHandler(AddressOf Click_Initilize this won&#39;t wok in vb and what is in the link&#39; http://blog.krisvandermast.com/AddingADynamicControlToAPlaceholderControlAndWireUpTheEvent.aspx I tried to put this in the int event but still nothing
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init Dim control As Control For Each control In phLinks.Controls If TypeOf control Is LinkButton Then link = CType(control, LinkButton) AddHandler link.Click, AddressOf Click_Initilize End If Next End Sub
any ideas?

ranigoal

Page_PreRende

all controls events happens before Page_PreRender....and especially dynamic controls need to be created either in page load or init...

Sequence of events in asp.net - http://forums.asp.net/t/1191194.aspx

That link is going to help you a lot....

Thanks.

[RESOLVED] Adding Horizontal Scroll Bar in ASP.NET TAble Control

Hi,

       I have a asp.net table ctrl named as tblctrl in my webpage. im inserting dynamiccontrols into this table. I have 30 rows with this table. i need to insert a horizontal scroll bar . for ist 15 rows we dont want to scroll the scrollbar.Please help me.

 Thanks

make a try on this stylesheet :

overflow-x: auto;

have a good time!

Hi arunpulikkan,

    For adding a scroll bar to a table, you can place the table into a div section. And set the width of the div and set overflow to auto. For example:

<div style="overflow :auto ;width:50px" >

<asp:Table ID="table1" runat="server">

</asp:Table>

</div>

     Or you can use Panel control which is rendered as div as well at the client side, and then add your Table control into it.

    <style type ="text/css" >
    .divclass
    {
    overflow:auto;
    }
    </style>

<asp:Panel ID="Panel1" runat="server" Width ="50px" CssClass="divclass">

</asp:Panel>

Regards,

Ivan.

thanks it solve my purpose too.