Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 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
<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
Updated 29-May-17 18:06pm
v2

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.
ASP.NET
<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">
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>
C#
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; }
    }
 
Share this answer
 
v4
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
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900