Click here to Skip to main content
13,558,743 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have a gridview control like following in the asp page and shows the picture and username from the userdetails. now question is how can i get the username when i click when i click on any of the username in the gridview. Maybe used javascript or c# code. Dont use any command field or any other control. just click on the gridview and show the username in another textbox. please help me as soon as possible.is there any one can help me?
<asp:gridview id="gv" showheader="false">
<columns>
  <templatefield>
        <itemtemplate>
            <asp:image runat="Server" imageurl='<%# Eval("pictpath") %>'/>
        </itemtemplate>
  </templatefield>
    <templatefield>
        <itemtemplate>
            <asp:Label runat="server" text='<%% Eval("username") %>'/>
        </itemtemplate>
    </templatefield>
</columns>
[Edit]Code block added[/Edit]
Posted 31-Mar-13 3:31am
Updated 29-May-17 18:06pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

asp.net grid view render html table. So when you use javascript then you should travase html table. The following code spinet will help you to understand the process.
<head runat="server">
    <title></title>
    <script src="jquery.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:gridview id="gv" showheader="false" runat="server" autogeneratecolumns="False" name="grdData" xmlns:asp="#unknown">
                <columns>
                    <asp:templatefield>
                        <itemtemplate>
                            <asp:image id="imgPicPath" runat="Server" alternatetext="Missing" imageurl="<%# Eval("pictpath") %>" />
                        </itemtemplate>
                    
                    <asp:templatefield>
                        <itemtemplate>
                            <asp:label id="lblUserName" runat="server" text="<%# Eval("username") %>" />
                        </itemtemplate>
                    
                     <asp:templatefield>
                        <itemtemplate>
                            <asp:label id="lblUserId" runat="server" text="<%# Eval("Id") %>" />
                        </itemtemplate>
                    
                </columns>            
        </div>
        <input type="button" id="btnShow" value="show" onclick="showGrid();"/>
    </form>
    </script>
</body>

<script type="text/javascript">
function showGrid() {
           var tbl = $("[id$=gv]");
           var rows = tbl.find('tr');
           for (var index = 0; index < rows.length; index++) {
               var row = rows[index];

               var image = $(row).find("img");
               var userName = $(row).find("[id*=lblUserName]").text();
               var userId = $(row).find("[id*=lblUserId]").text();

               alert(userName + " - " +  userId);
           }
       }

</script>
public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                gv.DataSource = GetData();
                gv.DataBind();
            }
        }

        IList<userinfo> GetData()
        {
            return new List<userinfo>
                {
                    new UserInfo{username = "A", pictpath = "a.jpg", Id = "111"},
                    new UserInfo{username = "B", pictpath = "b.jpg", Id = "222"},
                    new UserInfo{username = "C", pictpath = "c.jpg", Id = "333"},
                };
        }
    }
    public class  UserInfo
    {
        public string username { get; set; }
        public string pictpath { get; set; }
        public string Id { get; set; }
    }
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

how to find textbox value inside of grid view using javascript
in case of label, the above code is working fine
but when i am trying for text box it did not work

can you provide me solution for textBox

thank you
  Permalink  
Comments
Kats2512 30-May-17 5:07am
   
why did you have to resurrect such an old question? could you have not just asked a new 1 instead?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02-2016 | 2.8.180515.1 | Last Updated 30 May 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100