[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Iterating through a Span Collection in ASP.net v2

Posted on 2009-02-23
6
Medium Priority
?
376 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Craig Lambie
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:_Stilgar_
ID: 23720629
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).
0
 
LVL 1

Author Comment

by:Craig Lambie
ID: 23727737
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

0
 
LVL 14

Expert Comment

by:_Stilgar_
ID: 23728098
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.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Author Comment

by:Craig Lambie
ID: 23728598
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

0
 
LVL 14

Accepted Solution

by:
_Stilgar_ earned 750 total points
ID: 23732421
Either that, or cause a postback on each click (partial postback [=AJAX] will make things go smoother).

The code for both approaches is very simple, and is not something generic I can just write for you to use. The HiddenField way would probably just be something like:

var obj = $get("myHdnFld");
obj.value+="," + fileName;

And then in code (C#), after postback (for example, when all the form was filled in):

string[] sValues=myHdnFld.Value.Split(",");
foreach (string s in sValues) {
// Do your magic with s here
}
0
 
LVL 1

Author Comment

by:Craig Lambie
ID: 23738980
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question