Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET2.0
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
Edited 31-Mar-13 3:34am
ProgramFOX119.1K
v2

1 solution

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

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



Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 31 Mar 2013
Copyright © CodeProject, 1999-2014
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