Home > OS >  Dynamically Created Code blocks not firing
Dynamically Created Code blocks not firing

Time:09-16

  1. I need this to work on an image button that is dynamically created in a Child Div of a Parent div Container.

  2. I know the Code itself works because I have tested it on a non dynamically created image button

  3. All Image Buttons get a unique ID when they are created i.e. immgbttn1, immgbttn2 - based on the rowID of the record set. the divs themselves do not get unique ID's on the buttons

  4. the code shows up on the ASPX but does not fire.

  5. Screenshot of the app ![webform view]: (https://1drv.ms/u/s!AmmK5IyEVLSlg9gQSd_eb5KzA8UVcg?e=8sSdvI)

 Try
'this is within query the loop that counts the rows in the recordset and gets the data to 'name controls and onClick event handlers

                        Dim csName As [String] = "ClientScript" & RecID
                        Dim csType As Type = divleft.[GetType]()
                        Dim cs As ClientScriptManager = Page.ClientScript
                        If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
                            Dim csText As New StringBuilder()
                            csText.Append("<script language=" & Chr(34) & "vb" & Chr(34) & "runat=" & Chr(34) & "server" & Chr(34) & ">")
                            csText.Append("Sub ImageBttn" & RecID & "_Click(sender As Object, e As ImageClickEventArgs) Handles ImageBttn" & RecID & ".Click" & Chr(10) &
                   "Dim JobName As String = ImageBttn" & RecID & ".DescriptionUrl" & Chr(10) & "Dim JobID As String = ImageBttn" & RecID2 & ".CommandName" & Chr(10) &
                    "Response.Redirect(String.Format(" & Chr(34) & "~/476.aspx?JobName={0}&JobID={1}" & Chr(34) & ", JobName, JobID))" & Chr(10) &
                    "msgbox(ImageBttn" & RecID & ".DescriptionUrl)" & Chr(10) & "End Sub" & Chr(10) & "</script>")
                            cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
                        End If

                    Catch ex As Exception
                        MsgBox("Something has gone wrong..." & vbNewLine & ex.Message)

                    End Try
'this is what it looks like on the ASP side


        <script language="vb" runat="server">
             Sub ImageButton4_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton4.Click
                 Dim JobName As String = ImageButton4.DescriptionUrl
                 Dim JobID As String = ImageButton4.CommandName
                 Response.Redirect(String.Format("~/476.aspx?JobName={0}&JobID={1}", JobName, JobID))
             End Sub
</script>

CodePudding user response:

Use a data Repeater control with a Template

 <asp:Repeater ID="Repeater1" runat="server">
     <ItemTemplate>
     <div style="float:left;width:auto;border:solid;border-width:.25px;padding:15px;margin-right:20px;margin-bottom:20px;margin-top:20px;border-radius:10px;box-shadow: 5px 5px #888888">
        <div style="text-align:center">
            <img src="Content/Skyline.jpg" />
            <br />
            <asp:Label ID="txtHotel" runat="server" Text='<%# Eval("NumberName") %>'></asp:Label>
            <br />
            <asp:Label ID="txtAD" runat="server" Text='<%# Eval("JobAddress") %>'></asp:Label>
            <br />
            Active :<asp:CheckBox ID="Active" runat="server"      Checked ='<%# Eval("EstimateStatus") %>'></asp:CheckBox>
            <asp:Button ID="cmdTest" runat="server" Text="Row Click" OnClick="cmdTest_Click"
                PKID = '<%# Eval("Id") %>'
                />
            <br />
            <div style="float:left">
            <asp:ImageButton ID="cmdStatus" runat="server" ImageUrl="Content/ok.png" Height="20px" Width="20px"/>
            </div>
        </div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </ItemTemplate>
    </asp:Repeater>
  • Related