Link to home
Start Free TrialLog in
Avatar of Craig Lambie
Craig LambieFlag for Australia

asked on

Iterating through a Span Collection in ASP.net v2

Hi Experts,

I have a DIV with the potential to have several SPANs inside it, added on the client side via Javascript.  On the Server Side script processing of the Data, I want to input the Title of the SPAN collection into the Db.

I therefore need to iterate through the DIVs child SPANs.

I can't figure out how to do this in VB.net.  Please Help...
Dim els As System.Web.UI.HtmlControls.HtmlGenericControl
                    
                    els = Me.divFilesEdit
 
                    For i = 0 To els.InnerHtml.Count - 1
                        'Relate Files to Emails
                        ds = fADOCom("spWWT_AddReply_AddRelEmailFile", _
                             "intEmailID,intFileID,intOrder", _
                             strEmailID & "," & els.InnerHtml.ElementAt(i).Title.ToString & "," & i, _
                             1)
                    Next

Open in new window

Avatar of _Stilgar_
_Stilgar_
Flag of Israel image

The region - ASP - is incorrect, it should be ASP.NET.

How are the spans added on the client-side? unfortunately for you, unless you're using a postback (partial usign UpdatePanel or full) this won't be doable, since .NET doesn't recognize HTML elements which do not have the runat="server" applied, meaning every HTML element you'll add client-side and which will not have an ID generated by the server, will not be recognized.

So, the only two options you have are either to do the populating with postbacks (using spans/divs with runat="server" or .NET controls), or to use another technique using a HiddenField which will hold the names of the divs (so you'll actually iterate through the names in the HF and not through the spans collection).
Avatar of Craig Lambie

ASKER

Hi Stilgar,

I couldn't find an ASP.net zone when I asked the question sorry, ...

I ended up doing it this way.... out of interest...



                Dim dom As New System.Xml.XmlDocument
                Dim strXML = Replace(Me.divFilesEdit.InnerHtml.ToString, " ", "")
 
                dom.LoadXml("<xmla>" & strXML & "</xmla>")
 
                For i = 0 To dom.ChildNodes(0).ChildNodes.Count - 1
                    'Relate Files to Emails
                    ds1 = fADOCom("spWWT_AddReply_AddRelEmailFile", _
                                 "intEmailID<intFileID<intOrder", _
                                 strEmailID & "<" & dom.ChildNodes(0).ChildNodes(i).Attributes("title").Value & "<" & i, _
                                 1)
                Next

Open in new window

My solution seems to be much more straight forward and standard. Even if you're not using it, I think it worths being added to the KB.
I agree a solution should be added to the KB.

As you can see here from the JavaScript function, I have added a Span to the Div each time this function is run.

So.... what you are suggesting is to add to this process, by adding the Span ID attribute as a CSV to a Hidden field on the page which has runat="server" set, and then in the Server Side code split that into an array and get the info out somehow?

I am not a huge .net person, hence the question.  But I still think my idea works better, happy to give you the points if you code up the solution for the KB, I probably won't use it, as I like the XML way I did it, as the issues I have run into with JS and asp controls with runat="server" set are to many to speak of already in my early days as a .net dev.

C
function addFileLink(slFiles)
{
    var link = strURL + slFiles.options[slFiles.selectedIndex].innerHTML;
    var FileID = slFiles.options[slFiles.selectedIndex].value;
    var divFilesEdit = document.getElementById("<%=divFilesEditCID %>");
    
    //divFilesEdit.innerHTML = divFilesEdit.innerHTML + "<span id='spanFile_" + countFiles + "'><a id='aFile_" + countFiles + "' href='" + link + "' target='_blank'>" + link + "</a>&nbsp;&nbsp;&nbsp;<a href='#' onclick='removeFile('" + countFiles + "')'>Remove</a><br /></span>";
    fileArrayEdit.push("<span id='spanFile_" + countFiles + "' title='" + FileID + "'><a id='aFile_" + countFiles + "' href='" + link + "' target='_blank'>" + link + "</a><spaces>&nbsp;&nbsp;&nbsp;</spaces><a href='#' onclick='removeFile(" + countFiles + ");'>Remove</a><br/></span>");
    fileArrayHtml.push("<span id='spanFile_" + countFiles + "' title='" + FileID + "'><a id='aFile_" + countFiles + "' href='" + link + "' target='_blank'>" + link + "</a><br/></span>");
    
    var inputFilesHtml = document.getElementById("inputFilesHtml");
    
    inputFilesHtml.value = "";
    
    for(i=0; i<fileArrayHtml.length; i++)
    {
        inputFilesHtml.value = inputFilesHtml.value + fileArrayHtml[i] + ",";
    }
    if(inputFilesHtml.value.length > 0) inputFilesHtml.value = inputFilesHtml.value.substring(0, inputFilesHtml.value.length - 1);
    
    var inputFilesEdit = document.getElementById("inputFilesEdit");
    inputFilesEdit.value = "";
    divFilesEdit.innerHTML = "";
    for(i=0; i<fileArrayEdit.length; i++)
    {
        inputFilesEdit.value = inputFilesEdit.value + fileArrayEdit[i] + ",";
        divFilesEdit.innerHTML = divFilesEdit.innerHTML + fileArrayEdit[i];
    }
    if(inputFilesEdit.value.length > 0) inputFilesEdit.value = inputFilesEdit.value.substring(0, inputFilesEdit.value.length - 1);
    countFiles++;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of _Stilgar_
_Stilgar_
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Stilgar,

I still like my way better.  As in the Client side you can add/ delete the SPANs in the JS using their IDs, in your method, finding the ID in the Hidden field, and deleting it requires a loop through instead of just a $get(id).

I think for rich client side Javascript apps this method is better as the limitations of Postback and hidden fields is to great.   Pure AJAX using the HTTP object seem much smoother in my experience.