Solved

JavaScript - adding/removing items from a ListBox inside a web user control

Posted on 2004-08-04
7
52,123 Views
Last Modified: 2012-06-21
Hello Experts

I am looking at the question and answer:
http://www.experts-exchange.com/Web/Web_Languages/Q_20916437.html

I created a web form (.aspx) playing around with the examples given in the question above; its working fantastically.

Now I want to use the same example in a web user control (.ascx) form. Im new at this and cant find how you set the object variable: “var formObject = document.testForm”

I get the following error “Microsoft JScript runtime error: 'ListBox' is null or not an object”.

Thank for the help in advance.
Deon
0
Comment
Question by:deon001
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:nepali
ID: 11727328
u have to use the user control with a fixed ID on the aspx page. because javascript on the user control will work with cient side ids of the server controls.
0
 

Author Comment

by:deon001
ID: 11742098
In the example the line: “var formObject = document.testForm” looks at the form id. I’m new at this so help me if I have it wrong; so “testForm” is the form id, but a user control does not have a “<form></form>” so there is no id?

How would I change this example to work as a user control?
http://www.experts-exchange.com/Web/Web_Languages/Q_20916437.html)

Thank for the help!
0
 
LVL 7

Accepted Solution

by:
nepali earned 100 total points
ID: 11750695
HI deon, here are the guide lines...
1) The aspx pages have a form id.. u place the user control in the aspx page and inside the <form></form> body.
2) Now the javascript acting on the controls is in the user control. hence to access the controls u will need to use
  a)document.<formid>.<elementid> or
  b)document.getElementByID(<elementid>).
3) Normally (a) is used. Hence u need to hardcode the form name in the user control and in all the aspx pages where u have used the user control their respective forms should have the same form id.

Regards
Mahendra
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:deon001
ID: 11760642
Thanks for all the help it’s working great after I added the same form id to the user control as the page where the user control is on.

I have one quick question: Can I load the list from the server and then use JavaScript to remove items? (The moment I add - runat="server" the JavaScript gives problems) How do I approach this problem?
0
 
LVL 7

Expert Comment

by:nepali
ID: 11760684
Hi Deon,
Javascript doesn't understand runat=server. The problems usually u might face face are the events for a server control are by default on the server side. so even if say onclick=abc() for a server control the abc event method will be looked on the server side and not on the client side. So if there is an event problem with javascript on server controls then u can use either of the following cases.

1) either use an HTML Server control i.e <select id=aa runat=server></select> instead of <asp:dropdownlist>
or
if u r using <asp:dropdownlist> for features like autopostback an others then whenever the server control loads use the Attributes property to add client side events to the server control.

Regards
Mahendra
0
 

Author Comment

by:deon001
ID: 11761754
Hi Mahendra, Thanks again for the quick help :-)

All I want to do is delete an item from the list when the button is clicked. But is gives me the next error: “Microsoft JScript runtime error: 'fruitList.selectedIndex' is null or not an object.”
It works when I remove the runat=“server” and populate the listbox inside the HTLM code?

  Private Sub Page_Load(.......
        Me.fruitListID.Items.Add("Item 1")
        Me.fruitListID.Items.Add("Item 2")
        Me.fruitListID.Items.Add("Item 3")
        Me.fruitListID.Items.Add("Item 4")
        Me.fruitListID.Items.Add("Item 5")
        Me.fruitListID.Items.Add("Item 6")
    End Sub

-------------------------------------------------------------

<HTML>
      <HEAD>
<script type="text/javascript">
  function deleteOption(selectObject,optionRank)
  {
    if (selectObject.options.length!=0)
    {
            selectObject.options[optionRank]=null
    }
  }
  function testDelete()
  {
    var formObject = document.Main
    if (formObject.fruitList.selectedIndex!=-1)
    {
        deleteOption(formObject.fruitList,formObject.fruitList.selectedIndex)
    }
    else
    {
        alert("Select an option and click Delete")
    }
  }
</script>
      </HEAD>
      <BODY>
            <FORM id="Main">
                  <select size="10" name="fruitList" id="fruitListID" runat="server">
                  </select>
                  <input onclick="testDelete()" type="button" value="Delete"></TD>
            </FORM>
      </BODY>
</HTML>
0
 
LVL 7

Expert Comment

by:nepali
ID: 11761820
u have to use fruitListID and not fruitList
Whenever u get a  "null or not an object"...error try to put alert on the object name and see where javascript is able to recognize the object or not...

Regards
Mahendra
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
[YDN-DB] Delete DB then recreate and load when page is loaded/reloaded but not when refreshed 1 107
Lines Not Lining Up 2 62
WCAG audit tools 1 72
Problem to Popup 2 75
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now