Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone.

I have a grid view with link button.

i just want,

when i click a button in grid view , the value should be display in Label.

This image will show my problem


Code Behind:

C#
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

            switch (e.CommandName)
            {
                case "y":
                    {
                        string FirstColumnFieldName = e.CommandArgument.ToString();

                        Label1.Text=FirstColumnFieldName;

                        break;
                    }
                default:
                    break;
            }

            cmd = new SqlCommand("delete from Withdraw where UserName='FirstColumnFieldName'", con);
            cmd.ExecuteNonQuery();


    }


and in Asp Page :

  <asp:GridView ID="GridView1" runat="server" 
            onselectedindexchanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand" CellPadding="3" 
                                Width="609px" GridLines="None" BackColor="White" BorderColor="White" 
                                BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1" HorizontalAlign="Center">
                                <RowStyle BackColor="#DEDFDE" ForeColor="Black" HorizontalAlign="Center" 
                                    VerticalAlign="Middle" />
            <Columns>
              <asp:TemplateField HeaderText="Payment" HeaderStyle-CssClass="title_bg" 
                    ItemStyle-HorizontalAlign="Center">
            <ItemTemplate> 
                <asp:LinkButton ID="lkDelte" runat="server" OnClientClick="return confirm('Are you sure that Payment is Done ?')"
                      CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
                      CommandName="y" ><img src="Images/A5.gif" alt="PayPal" border="0" /></asp:LinkButton>
            </ItemTemplate> 

<HeaderStyle CssClass="title_bg"></HeaderStyle>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:TemplateField>
</Columns>

                                <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                                <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" 
                                    VerticalAlign="Middle" />
                                <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" 
                                    HorizontalAlign="Center" VerticalAlign="Bottom" />

                                </asp:GridView>




Thanks....
Posted
Updated 2-May-12 3:54am
v5
Comments
Sandeep Mewara 2-May-12 8:59am    
Click link would display what value? Full row?

Did you try anything?
Pr!y@ 2-May-12 9:55am    
Sorry,I forget to write my test code

When you use the gridview's rowheader clicked event then you can access the clicked row and all its cells. Then you can use that value to populate a label easily. If you need then you can use gridview's cellmouseclick event also. Just google it for code.
 
Share this answer
 
Try:
Following binds the ItemName to the CommandArgument of the LinkButton.
XML
<asp:LinkButton ID="LinkButton1" runat="server" Text="Add to cart"
   OnClick="LinkButton1_Click" CommandArgument='<%# Eval("ItemName") %>' />


You can then retrieve it like this in the code behind:
C#
protected void LinkButton1_Click(object sender, EventArgs e)
{
  LinkButton myButton = sender as LinkButton;
  if (myButton != null)
  {
     string title = myButton.CommandArgument;
     // use this title now to set as a text for your label.
  }
}

The sender holds a reference to the LinkButton that triggered the event handler.
You can cast the object into a LinkButton and then retrieve its CommandArgument
 
Share this answer
 
v3
Comments
Pr!y@ 2-May-12 14:30pm    
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton myButton = sender as LinkButton;
if (myButton != null)
{
string title = myButton.CommandArgument;
// use this title now to set as a text for your label.
}

else
{
Response.Write("Not Show");
}
}






Hello Sir in solution else statement is executing not if....

Why?

while i am clicking button and it has vaues in collumn
Sandeep Mewara 3-May-12 1:07am    
So you say that LinkButton1_Click event is raised but the sender is not LinkButton? :doh:
Pr!y@ 3-May-12 6:02am    
Yes sir when i click LinkButton1 . Event is raised but value of that row's first column is not displaying in Label1. else statement is executing.

code:

protected void LinkButton1_Click(object sender, EventArgs e)
{

LinkButton myButton = sender as LinkButton;

if (myButton != null)
{
string title = myButton.CommandArgument;
// use this title now to set as a text for your label.
Label1.Text=title;
}
else

{ Response.Write("Not Show");
}

}

<asp:gridview id="GridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" onrowcommand="GridView1_RowCommand" xmlns:asp="#unknown">
 <columns>
              <asp:templatefield headertext="Payment" headerstyle-cssclass="title_bg">
                    ItemStyle-HorizontalAlign="Center">
            <itemtemplate> 
                <asp:linkbutton id="lkDelte" runat="server" onclientclick="return confirm('Are you sure that Payment is Done ?')">
                      CommandArgument='<%# Eval("UserName") %>'
                      ><img src="Images/A5.gif" alt="PayPal" border="0" /></asp:linkbutton>
            </itemtemplate> 

<HeaderStyle CssClass="title_bg"></HeaderStyle>

<itemstyle horizontalalign="Center"></itemstyle>
        </asp:templatefield>
</columns> </asp:gridview>





C#
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
   {

          Label3.Text = GridView1.Rows[index].Cells[1].Text;
   }
 
Share this answer
 

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