0

I have created a blank asp.net forms website. I have taken the default.aspx page and replaced the default code with the code from

https://blueimp.github.io/jQuery-File-Upload/jquery-ui.html

So it looks like

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
HTML / Javascript from the link above. 
</asp:Content>

If i strip out the master file content and just paste the content of the page it works perfectly. So for some reason the

When i click add files and browse the files do not show up on the page, but when i have no master file stuff it will show up fine.

I understand this isnt great debug information. If anyone knows a good way i cold even debug this it would be great.

If i were to guess it appears the following code is the cause of the error.

<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
      {% console.log(file.name); %}
    <tr class="template-upload fade">
        <td>
            <span class="preview"></span>
        </td>
        <td>
            <p class="name">{%=file.name%}</p>
            <strong class="error"></strong>
        </td>
        <td>
            <p class="size">Processing...</p>
            <div class="progress"></div>
        </td>
        <td>
            {% if (!i && !o.options.autoUpload) { %}
                <button class="start" disabled>Start</button>
            {% } %}
            {% if (!i) { %}
                <button class="cancel">Cancel</button>
            {% } %}
        </td>
    </tr>
{% } %}
</script>

When i have a look at the html through the browser the following form is missing from the page when i load it with the master file

<form id="fileupload" action="sendfiles" method="POST" enctype="multipart/form-data">
Dan Hastings
  • 3,241
  • 7
  • 34
  • 71
  • What does you master page look like. Could you be trying to access a resource before it is defined in the master file? – Ross Bush Jul 13 '15 at 17:23
  • A lot of this code is bad practice, you shouldn't loop like that in your aspx, instead you should use an ASP:Repeater control to iteratively output table columns, rows, lists, etc etc. – Ryan Mann Jul 13 '15 at 17:28
  • you also shouldn't use raw input elements and should instead use ASP:Button or ASP:TextBox, etc etc etc. – Ryan Mann Jul 13 '15 at 17:28
  • My best recommendation is to ditch Web Forms and use MVC 5 unless you are required to use webforms. – Ryan Mann Jul 13 '15 at 17:29
  • Also it appears you are using some sort of client side JS framework.... You should add a tag for that. It's not angular, as such I am not sure what it is. – Ryan Mann Jul 13 '15 at 17:55
  • Or you can set the Client ID to not change by adding the following: `ClientIDMode="Static"` in the `RadioButton` tag. – SearchForKnowledge Jul 13 '15 at 19:49

2 Answers2

1

When dealing with ASPX never use raw html id's in your javascript, you need to look up their real id via,

<script>
    var controlId = '<%= this.SomeControl.ClientId %>'
    var things = $(controlId);
</script>

The <%=... %> logic is an ASPX expression, the server side aspx engine will see it and convert it into what you are asking for when it spits it out to the response.

Ryan Mann
  • 5,178
  • 32
  • 42
  • You can use raw ID's, you just need to change the ClientIDMode. – mason Jul 13 '15 at 17:27
  • Yeah you can do that to and chance it back to the old version, it's a webconfig setting, forget where or what exactly it's called though. Personally I prefer to use the epxressions because then it will work regardless of the client id mode. – Ryan Mann Jul 13 '15 at 17:30
  • would the fact that im using a html form inside the code cause an issue? I had a look at the code on the page and the following is missing from the page.
    – Dan Hastings Jul 13 '15 at 17:35
  • In ASPX you cannot use your own Form Elements. You can not ever use a form element. ASPX is designed to handle the form tag on it's own and it wraps every page in one Form Tag and every control on the page posts back to that one forms form data "called ViewData in aspx". In aspx you just add controls, that have server side logic and those controls tie into the pages main Form Tag. – Ryan Mann Jul 13 '15 at 17:47
  • If you need to make an ajax call to something on the page, you either use An UpdatePanel and let it handle it for you, or create a GenericHttp Handler and use that for your ajax. e.g. /somesight/someHandler.ashx Generic http handlers do not have page bodies, they are designed to return whatever you want via their content type. You can render an On the Fly Image, Json, Xml, etc etc etc. Then on your main page your jQuery can make an Ajax call to that. – Ryan Mann Jul 13 '15 at 17:50
0

Now that you've identified the missing form - Asp.Net webforms does not allow multiple elements. It's an annoyance that comes with webforms, but there are some workarounds that can be found on the first answer here, along with a good discussion about the issue: Can we use multiple forms in a web page?

Previous answer - Check out the source of the generated HTML in your browser (F12). My guess, with what little information is available, is that the IDs of elements that you are targeting with jQuery are being changed in children of the master page.

Community
  • 1
  • 1
Dave 5709
  • 605
  • 3
  • 11
  • I second this. In ASP.net webforms, the ASPX engine modifies your html id's depending on how deep they are in the Control heirarchy. When you add a master page your page is being rendered inside the master page's control. Thus all the ids on the page using the master page change. – Ryan Mann Jul 13 '15 at 17:24
  • should this matter though? The js above simply outputs code so it shouldnt matter too much? – Dan Hastings Jul 13 '15 at 17:30
  • @DanHastings You didn't show your JS, so how could we know that? – mason Jul 13 '15 at 17:31
  • @DanHastings Were you able to apply one of the workarounds, or another method to get this working? – Dave 5709 Jul 16 '15 at 17:21