Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get Listview SelectedItem from JavaScript

Posted on 2009-04-05
3
Medium Priority
?
4,412 Views
Last Modified: 2012-05-06
Hi,

How do I get the current ListView item in JavaScript?

I have a listview and wish to do validation and and additional data lookup when a user exits various fields contained within the listview item.  But I cannot figure out how to determine which listview item is the current one being edited.  The code sample below illustrates what I am attempting to accomplish, however the javascript only works for inserts or if the item being edited is the last item in the listview.  Of course the problem is when a user attempts to edit an item other than the last one this code will not work properly.

Thanks!
<asp:ListView ID="ListView1">
       <EditItemTemplate>
            <table style="border-bottom: 1px outset #ffffff">
                <tr>
                <td>
                    <asp:TextBox ID="Place_of_Service" runat="server" 
                        style="color:#000000; text-align:center;border: #000000 solid 1px;" 
                        Text='<%# Bind("[Place of Service]") %>' onblur="javascript:POS_OnBlur();" />
                </td>
                <td>
                    <asp:TextBox ID="Type_of_Service" runat="server"
                        style="color:#000000; text-align:center;border: #000000 solid 1px;"  
                        Text='<%# Bind("[Type of Service]") %>' onblur="javascript:TOS_OnBlur();"/>
                </td>
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                        Text="Update" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Cancel" />
                </td>
            </tr>
            </table>
        </EditItemTemplate>
        <InsertItemTemplate>
                <table>
                <tr>
                <td>
                    <asp:TextBox ID="Place_of_Service" runat="server" 
                        style="color:#000000; text-align:center;border: #000000 solid 1px;" 
                        Text='<%# Bind("[Place of Service]") %>' onblur="javascript:POS_OnBlur();" />
                </td>
                <td>
                    <asp:TextBox ID="Type_of_Service" runat="server"
                        style="color:#000000; text-align:center;border: #000000 solid 1px;"  
                        Text='<%# Bind("[Type of Service]") %>' onblur="javascript:TOS_OnBlur();"/>
                </td>
                <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                        Text="Insert" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Clear" />
                </td>
            </tr>
            </table>
        </InsertItemTemplate>
</asp:ListView>
 
 
javascipt:
 
function TOS_OnBlur() {
    var tbl = document.getElementById("InputFormMaster_BodyContent_ListView1_Table2");
    var intRow = tbl.rows.length - 2;
    var ctl = "InputFormMaster$BodyContent$ListView1$ctrl" + intRow + "$Type_of_Service";
 
    if (document.getElementById(ctl).value != "") {
        var e = document.getElementById(ctl).value;
        
        if (e.toUpperCase == "UC")
            document.getElementById(ctl).value = e.toUpperCase();
        if (isNaN(e) == false) {
            if (e.length == 1)
                document.getElementById(ctl).value = "0" + e;
        }
        
        ValidationService.IsValidTOS(document.getElementById(ctl).value, IsValidTOS_Complete, ErrorHandler);
    }
 
}

Open in new window

0
Comment
Question by:TerryDean
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 24072534
Change the function to this

onblur="javascript:TOS_OnBlur(this.id);"

function TOS_OnBlur(id) {
var ctl = id;
}
0
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 24072539
Change the function to this

onblur="javascript:TOS_OnBlur(this.id);"

function TOS_OnBlur(id) {
var ctl = id;
}
0
 

Author Closing Comment

by:TerryDean
ID: 31566800
Thanks, works perfectly!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

670 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