Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
in which event can i get the row details of a gridview.in my page i am having one gridview on that i am taking one link button on every row with student_id by cliking on the student_id link button i am getting the value of the particular Student_id which i clicked. here iw ant at the same time name of that student_id down i am posting the code.

//html code

XML
<asp:GridView ID="gridviewCandidateDetails" runat="server"
        AutoGenerateColumns="False" BackColor="White" BorderColor="White"
        BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
        GridLines="None" Width="783px" >
        <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
        <Columns>
            <asp:TemplateField HeaderText="Student_ID">
                <ItemTemplate>
                <asp:LinkButton ID="lblStudent_ID" runat="server"
                         Text='<%#Eval("Student_ID")%>' onclick="lblStudent_ID_Click"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Child_Name">
                <ItemTemplate>
                    <asp:Label ID="lblChild_Name" runat="server" Text='<%#Eval("Child_Name")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Class">
                <ItemTemplate>
                    <asp:Label ID="lblClass" runat="server" Text='<%#Eval("Class")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Phone">
                <ItemTemplate>
                    <asp:Label ID="lblPhone" runat="server" Text='<%#Eval("Phone")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Gender">
                <ItemTemplate>
                    <asp:Label ID="lblGender" runat="server" Text='<%#Eval("Gender")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
        <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
    </asp:GridView>



//ASP.NEt code

C#
protected void lblStudent_ID_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            Session["lbtnStudent_id"] = Convert.ToString(lbtn.Text);
            gridviewCandidateDetails.Visible = false;
            PanelFeeTypes.Visible = true;
            lblSelectMonth1.Visible = false;
            ddlSelectMonth1.Visible = false;
        }
Posted
Comments
Nandakishore G N 9-Mar-13 4:29am    
use gridview_rowcommand..

1 solution

XML
<asp:TemplateField HeaderText="Student_ID">
         <ItemTemplate>
               <asp:LinkButton ID="lblStudent_ID" runat="server"
               Text='<%#Eval("Student_ID")%>' CommandName="StudentID">        </ItemTemplate>


Modify your source code as I shown above.I suggest don't go for button click event instead go for command name.
C#
Private gridviewCandidateDetails_ItemCommand(object sender, GridCommandEventArgs e)
{
     if(e.CommandName=="StudentID") //Exactly same command name as given in source code
     {
       LinkButton lblStudent_ID=(LinkButton)e.Item.FindControl("lblStudent_ID");
       Label lblChild_Name=(Label)e.Item.FindControl("lblChild_Name");
       Label lblClass=(Label)e.Item.FindControl("lblClass");
       //Like this you can find all the row details.
     }
}
 
Share this answer
 
v2
Comments
ntitish 11-Mar-13 0:58am    
in my gridview properties window there is no event like
Private gridviewCandidateDetails_ItemCommand(object sender, GridCommandEventArgs e)
{

}

//i written code like this event and it is showing error

protected void gridviewCandidateDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Student_ID")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gridviewCandidateDetails.Rows[index];
Label lblChild_Name = (Label)row.FindControl("lblChild_Name");
lblName1.Text = lblChild_Name.Text;
Label lblClass = (Label)row.FindControl("lblClass");
lblClass1.Text = lblClass.Text;
}
}

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