Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2256
  • Last Modified:

get datalist selected item javascript

hi

i have datalist in aspx page and i want to get selected item value using java script at SetSelectedItemList() function

how can i do that ??
<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("itemPath") %>'
Text='<%# Bind("itemName") %> ' onclick="SetSelectedItemList();"></asp:HyperLink>
</td>

Open in new window

0
Rewaya
Asked:
Rewaya
  • 11
  • 10
  • 8
2 Solutions
 
sjklein42Commented:
Something like this should do it.  Pass "this" to SetSelectedItemList, and use it inside the routine to get back to the properties (ie. value) for the selected item.

<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("itemPath") %>'
Text='<%# Bind("itemName") %> ' onclick="SetSelectedItemList(this);"></asp:HyperLink>
</td>


function SetSelectedItemList(thisItem)
{
	var thisValue = thisItem.value;
	...
}

Open in new window

0
 
RewayaAuthor Commented:
value of var thisValue = thisItem.value;  is undefined :(
0
 
chaitu chaituCommented:
var thisValue = thisItem.text;
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
sjklein42Commented:
Which value do you want to get back for the link?  I don't see a simple "value" property.  Are you trying to get back the "Text"?

Let's try this inside the function and see if you get back the Text value:

alert(this);    // should show [object]
var thisValue = thisItem.Text;
alert(thisValue)

Open in new window

0
 
RewayaAuthor Commented:
still undefined

at first  alert(this);    // should show [object   Show [Object Window]
second  alert(thisValue)                                 Show undefined
 
0
 
chaitu chaituCommented:
can you post HTML code here??
0
 
RewayaAuthor Commented:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DirectoreyFiles.aspx.cs"
    Inherits="DirectoreyFiles" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style4
        {
            width: 670px;
        }
        .style3
        {
            width: 100%;
        }
        .style5
        {
            width: 70px;
        }
        .style6
        {
            width: 289px;
        }
        .style1
        {
            width: 100%;
            height: 119px;
        }
    </style>

    <script type="text/javascript">
        var path;

        function CreatNewFolder() {

            var filename = prompt("Please write folder Name", "NewFolder");
            try {
                var myObject, newfolder;
                myObject = new ActiveXObject("Scripting.FileSystemObject");
                path = decodeURIComponent(path);
                newfolder = myObject.CreateFolder(path + "//" + filename);
                alert("Folder created at " + path + "//" + filename);
            }
            catch (err) {
                alert(err);
            }
        }
        function SetPath() {
            if (location.search != "") {
                path = window.location.search;
                var parms = path.split('=');
                path = "<%=AppDomain.CurrentDomain.BaseDirectory.ToString().Replace(@"\", "/")%>" + parms[1];
            }
         }
         
         function SetSelectedItemList(thisItem)
         {
         var querystring = window.location.search;
         alert("Folder created at "+querystring);
         //this.opener.document.getElementById("testx").value="xx";
         //((HiddenField)DataList1.SelectedItem.FindControl("hdnID")).Value
         alert(this);    // should show [object] 
         var thisValue = thisItem.Text;
         alert(thisValue)
         parent.hello(thisValue);

         }

//        function alertMe(hi) {
//            alert(hi);
//        }
//        function SetFolderPath(pathvalue) {

//            path = pathvalue;
//            alert(decodeURIComponent(path));
//            var baseDirectory = "<%=AppDomain.CurrentDomain.BaseDirectory.ToString().Replace(@"\", "/")%>";
//            alert(path);
//            alert(baseDirectory);
//        }

    </script>

</head>
<body onload="SetPath();">
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td valign="top" height="100%" class="style4" colspan="4">
                    <asp:DataList ID="DataList1" runat="server" Width="794px" BorderStyle="Solid" RepeatColumns="3"
                        RepeatDirection="Horizontal" RepeatLayout="Flow">
                        <ItemTemplate>
                            <table class="style3">
                                <tr>
                                    <td>
                                        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("itemPath") %>'
                                            Text='<%# Bind("itemName") %> ' onclick="SetSelectedItemList(this);"></asp:HyperLink>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:DataList>
                </td>
            </tr>
            <tr>
                <td valign="top" height="100%" class="style5">
                    <asp:Button ID="Button_CreateNewFolder" runat="server" Style="width: 100%" OnClientClick="CreatNewFolder();"
                        Text="Create New Folder" OnClick="Button_CreateNewFolder_Click" Width="70px" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
                <td valign="top" height="100%" class="style6">
                    <asp:Label ID="Label_UploadFile" runat="server" Style="text-align: center" Text="Upload a new file in this folder"></asp:Label>
                </td>
                <td valign="top" height="100%" class="style4">
                    <asp:FileUpload ID="FileUpload" runat="server" />
                    <br />
                    <asp:Label ID="Label_StatusLabel" runat="server" Text="Upload status: "></asp:Label>
                </td>
                <td valign="top" height="100%" class="style4">
                    <asp:Button ID="Button_Upload" runat="server" Text="UpLoad" Height="26px" OnClick="Button_Upload_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Open in new window

0
 
chaitu chaituCommented:
at 63 line you put  alert(this);    // should show [object]
put thisItem

 alert(thisItem);    // should show [object]
         var thisValue = thisItem.Text;
0
 
chaitu chaituCommented:
check this

  <script>
 function SetSelectedItemList(thisItem)
         {
       
     
         alert(thisItem);    // should show [object]
         var thisValue = thisItem.Text;
         alert(thisValue)
     
         }


  </script>
 </HEAD>

 <BODY>
  <input type='button' Text='sss' value='aaa' onclick='SetSelectedItemList(this)'>
 </BODY>
</HTML>
0
 
sjklein42Commented:
Thanks chaituu for pitching in.  We'll get to the bottom of this.

I think the asp tag is generating different tags than we're expecting.

Please see what this does:

function SetSelectedItemList(thisItem)
{
	var thisValue = thisItem.href;
	alert(thisValue);
}

Open in new window

0
 
RewayaAuthor Commented:
still undefined for each

var thisValue = thisItem.href;
var thisValue = thisItem.Text;
0
 
sjklein42Commented:
Just a thought.  When you look at your web page in a browser and "View Source", does it still show "asp" tags or have they been expanded into html <a> tags?  We may have better luck if we can see the "source" as seen by the browser rather than the raw source file.

Please paste the results of the View Source window here so we can see it, too.  Thanks.
0
 
chaitu chaituCommented:
paste your view source of html code...take right click of the page and copy the source paste it here.
0
 
RewayaAuthor Commented:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
 
</title>
    <style type="text/css">
        .style4
        {
            width: 670px;
        }
        .style3
        {
            width: 100%;
        }
        .style5
        {
            width: 70px;
        }
        .style6
        {
            width: 289px;
        }
        .style1
        {
            width: 100%;
            height: 119px;
        }
    </style>
 
    <script type="text/javascript">
        var path;
 
        function CreatNewFolder() {
 
            var filename = prompt("Please write folder Name", "NewFolder");
            try {
                var myObject, newfolder;
                myObject = new ActiveXObject("Scripting.FileSystemObject");
                path = decodeURIComponent(path);
                newfolder = myObject.CreateFolder(path + "//" + filename);
                alert("Folder created at " + path + "//" + filename);
            }
            catch (err) {
                alert(err);
            }
        }
        function SetPath() {
            if (location.search != "") {
                path = window.location.search;
                var parms = path.split('=');
                path = "E:/Rabtah Project/Study CMS Code/WebSite Study/UploadFiles/" + parms[1];
            }
         }
         
         function SetSelectedItemList(thisItem)
         {
         var querystring = window.location.search;
        // alert("Folder created at "+querystring);
         thisItem = decodeURIComponent(thisItem);
        // alert(thisItem);    // should show [object]
         var thisValue = thisItem.href; 
         alert (thisItem);         
         alert(thisValue)
         parent.hello(thisItem);
 
         }
 
//        function alertMe(hi) {
//            alert(hi);
//        }
//        function SetFolderPath(pathvalue) {
 
//            path = pathvalue;
//            alert(decodeURIComponent(path));
//            var baseDirectory = "E:/Rabtah Project/Study CMS Code/WebSite Study/UploadFiles/";
//            alert(path);
//            alert(baseDirectory);
//        }
 
    </script>
 
</head>
<body onload="SetPath();">
    <form name="form1" method="post" action="DirectoreyFiles.aspx?DirectoryName=Upload%2fImage%2fmarwaa&amp;Item_ParentCode=6&amp;Item_Order=3" id="form1" enctype="multipart/form-data">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjEzMDcxODgwNw9kFgICAw8WAh4HZW5jdHlwZQUTbXVsdGlwYXJ0L2Zvcm0tZGF0YRYCAgEPPCsACQEADxYEHghEYXRhS2V5cxYAHgtfIUl0ZW1Db3VudAIIZBYQZg9kFgICAQ8PFgQeC05hdmlnYXRlVXJsBVVFOi9SYWJ0YWggUHJvamVjdC9TdHVkeSBDTVMgQ29kZS9XZWJTaXRlIFN0dWR5L1VwbG9hZEZpbGVzL1VwbG9hZC9JbWFnZS9tYXJ3YWFcaG9waG9wHgRUZXh0BQZob3Bob3BkZAIBD2QWAgIBDw8WBB8DBVVFOi9SYWJ0YWggUHJvamVjdC9TdHVkeSBDTVMgQ29kZS9XZWJTaXRlIFN0dWR5L1VwbG9hZEZpbGVzL1VwbG9hZC9JbWFnZS9tYXJ3YWFca2pramtqHwQFBmtqa2pramRkAgIPZBYCAgEPDxYEHwMFU0U6L1JhYnRhaCBQcm9qZWN0L1N0dWR5IENNUyBDb2RlL1dlYlNpdGUgU3R1ZHkvVXBsb2FkRmlsZXMvVXBsb2FkL0ltYWdlL21hcndhYVxudWxsHwQFBG51bGxkZAIDD2QWAgIBDw8WBB8DBVRFOi9SYWJ0YWggUHJvamVjdC9TdHVkeSBDTVMgQ29kZS9XZWJTaXRlIFN0dWR5L1VwbG9hZEZpbGVzL1VwbG9hZC9JbWFnZS9tYXJ3YWFceWFyYWIfBAUFeWFyYWJkZAIED2QWAgIBDw8WBB8DBV1FOi9SYWJ0YWggUHJvamVjdC9TdHVkeSBDTVMgQ29kZS9XZWJTaXRlIFN0dWR5L1VwbG9hZEZpbGVzL1VwbG9hZC9JbWFnZS9tYXJ3YWFcNGI5ODc2MjI3My5naWYfBAUONGI5ODc2MjI3My5naWZkZAIFD2QWAgIBDw8WBB8DBVRFOi9SYWJ0YWggUHJvamVjdC9TdHVkeSBDTVMgQ29kZS9XZWJTaXRlIFN0dWR5L1VwbG9hZEZpbGVzL1VwbG9hZC9JbWFnZS9tYXJ3YWFcOS5qcGcfBAUFOS5qcGdkZAIGD2QWAgIBDw8WBB8DBWpFOi9SYWJ0YWggUHJvamVjdC9TdHVkeSBDTVMgQ29kZS9XZWJTaXRlIFN0dWR5L1VwbG9hZEZpbGVzL1VwbG9hZC9JbWFnZS9tYXJ3YWFcaG9waG9wXzJfXzEyLTA2LTA5XzIwMTUuanBnHwQFG2hvcGhvcF8yX18xMi0wNi0wOV8yMDE1LmpwZ2RkAgcPZBYCAgEPDxYEHwMFWEU6L1JhYnRhaCBQcm9qZWN0L1N0dWR5IENNUyBDb2RlL1dlYlNpdGUgU3R1ZHkvVXBsb2FkRmlsZXMvVXBsb2FkL0ltYWdlL21hcndhYVxUaHVtYnMuZGIfBAUJVGh1bWJzLmRiZGRkBMRglakJVeM+UlMFufWRDYvIGyU=" />
</div>
 
<div>
 
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwLq2OfWCgLFq5adBwK8qMuRDYkUoT/rulaawhi2EmEPU8w0npFG" />
</div>
    <div>
        <table class="style1">
            <tr>
                <td valign="top" height="100%" class="style4" colspan="4">
                    <span id="DataList1" style="display:inline-block;border-style:Solid;width:794px;"><span>
                            <table class="style3">
                                <tr>
                                    <td>
                                        <a id="DataList1_ctl00_HyperLink1" onclick="SetSelectedItemList(this);" href="E:/Rabtah%20Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/marwaa\hophop">hophop</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                        </span><span>
                            <table class="style3">
                                <tr>
                                    <td>
                                        <a id="DataList1_ctl01_HyperLink1" onclick="SetSelectedItemList(this);" href="E:/Rabtah%20Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/marwaa\kjkjkj">kjkjkj</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                        </span><span>
                            <table class="style3">
                                <tr>
                                    <td>
                                        <a id="DataList1_ctl02_HyperLink1" onclick="SetSelectedItemList(this);" href="E:/Rabtah%20Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/marwaa\null">null</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                        </span><br /><span>
                            <table class="style3">
                                <tr>
                                    <td>
                                        <a id="DataList1_ctl03_HyperLink1" onclick="SetSelectedItemList(this);" href="E:/Rabtah%20Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/marwaa\yarab">yarab</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                        </span><span>
                            <table class="style3">
                                <tr>
                                    <td>
                                        <a id="DataList1_ctl04_HyperLink1" onclick="SetSelectedItemList(this);" href="E:/Rabtah%20Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/marwaa\4b98762273.gif">4b98762273.gif</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                        </span><span>
                            <table class="style3">
                                <tr>
                                    <td>
                                        <a id="DataList1_ctl05_HyperLink1" onclick="SetSelectedItemList(this);" href="E:/Rabtah%20Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/marwaa\9.jpg">9.jpg</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                        </span><br /><span>
                            <table class="style3">
                                <tr>
                                    <td>
                                        <a id="DataList1_ctl06_HyperLink1" onclick="SetSelectedItemList(this);" href="E:/Rabtah%20Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/marwaa\hophop_2__12-06-09_2015.jpg">hophop_2__12-06-09_2015.jpg</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                        </span><span>
                            <table class="style3">
                                <tr>
                                    <td>
                                        <a id="DataList1_ctl07_HyperLink1" onclick="SetSelectedItemList(this);" href="E:/Rabtah%20Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/marwaa\Thumbs.db">Thumbs.db</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                        </span><br /></span>
                </td>
            </tr>
            <tr>
                <td valign="top" height="100%" class="style5">
                    <input type="submit" name="Button_CreateNewFolder" value="Create New Folder" onclick="CreatNewFolder();" id="Button_CreateNewFolder" style="width:70px;width: 100%" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
                <td valign="top" height="100%" class="style6">
                    <span id="Label_UploadFile" style="text-align: center">Upload a new file in this folder</span>
                </td>
                <td valign="top" height="100%" class="style4">
                    <input type="file" name="FileUpload" id="FileUpload" />
                    <br />
                    <span id="Label_StatusLabel">Upload status: </span>
                </td>
                <td valign="top" height="100%" class="style4">
                    <input type="submit" name="Button_Upload" value="UpLoad" id="Button_Upload" style="height:26px;" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Open in new window

0
 
chaitu chaituCommented:
u didnt write decodeURIComponent method..
thisItem = decodeURIComponent(thisItem);
0
 
chaitu chaituCommented:
function SetSelectedItemList(thisItem)
         {
             alert(thisItem.href)
         var querystring = window.location.search;
        // alert("Folder created at "+querystring);
      //   thisItem = decodeURIComponent(thisItem);
         alert(thisItem);    // should show [object]
         var thisValue = thisItem.href;
                alert(thisValue)
         parent.hello(thisValue);
 
         }
0
 
sjklein42Commented:
That wasn't the same as what we posted for you!

You were changing the value of thisItem inside the function.  That's a "no-no".  thisItem is the variable that points back to your original <a> tag.

This should work better:

         
         function SetSelectedItemList(thisItem)
         {
        // var querystring = window.location.search;
        // alert("Folder created at "+querystring);

         var thisValue = thisItem.href; 
         alert(thisValue)

         thisValue = decodeURIComponent(thisValue);
         parent.hello(thisValue);
 
         }

Open in new window

0
 
RewayaAuthor Commented:
ok now works Thanks
but little question how can i remove %20 and / backslash from   var thisValue = thisItem.href;
0
 
sjklein42Commented:
Not sure if you mean slash or backslash - this does both

<script>
var foo="abc%20def/gh\\ij"
alert(foo)
foo = foo.replace("%20","")
alert(foo)
foo = foo.replace("/","")
alert(foo)
foo = foo.replace("\\","")
alert(foo)
</script>

Open in new window

0
 
sjklein42Commented:
I think this is what you really want, though..  Turn the path into a DOS filename?

<script>
var thisValue="E:/Rabtah%20Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/marwaa\\hophop"
alert(thisValue)
thisValue = thisValue.replace(/\%20/g," ")
thisValue = thisValue.replace(/\//g,"\\")
alert(thisValue)
</script>

Open in new window

0
 
RewayaAuthor Commented:
var thisValue = thisItem.href;
        thisValue = decodeURIComponent(thisValue);
        thisValue = thisValue.replace("%20"," ");
        thisValue = thisValue.replace("file:///","");

E:/Rabtah Project/Study%20CMS%20Code/WebSite%20Study/UploadFiles/Upload/Image/New%20Folder/b3b3.txt

still exist %20

and also this not solve problem
alert(thisValue)
thisValue = thisValue.replace(/\%20/g," ")
thisValue = thisValue.replace(/\//g,"\\")
alert(thisValue)
0
 
sjklein42Commented:
Ok.  Instead of giving you small fragments of code, this is what I think the whole SetSelectedItemList function should look like.  Please try it just like this.  If it still doesn't work, please post what the output of the "alert" boxes showed.

function SetSelectedItemList(thisItem)
{
	var thisValue = thisItem.href; 
	alert(thisValue)
	thisValue = thisValue.replace(/\%20/g," ")
	thisValue = thisValue.replace(/\//g,"\\")
	alert(thisValue)
	parent.hello(thisValue);
}

Open in new window


PS.  Rewaya: In my first attempt I made a mistake using a quoted string as the first argument to the replace function.  It only replaces the first occurrence unless you use a regexp with the /g flag.
0
 
RewayaAuthor Commented:
Thnx very muchhhh
0
 
RewayaAuthor Commented:
still
file:\\\E:\Rabtah%2520Project\Study%2520CMS%2520Code\WebSite%2520Study\UploadFiles\Upload\Image\marwaa\4b98762273.gif
0
 
sjklein42Commented:
Ok.  With that info, this should do it:

function SetSelectedItemList(thisItem)
{
	var thisValue = thisItem.href; 
	alert(thisValue)
	thisValue = thisValue.replace(/^file\:\\*/ig,"")
	thisValue = thisValue.replace(/\%2520/g," ")
	thisValue = thisValue.replace(/\%20/g," ")
	thisValue = thisValue.replace(/\//g,"\\")
	alert(thisValue)
	parent.hello(thisValue);
}

Open in new window

0
 
RewayaAuthor Commented:
yessss now solved thank you very much  :)))
0
 
sjklein42Commented:
chaituu, enjoyed working with you.  Good teamwork.
0
 
chaitu chaituCommented:
me too..good luck..
0
 
RewayaAuthor Commented:
chaituu and sjklein42 enjoyed learning from you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now