Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
i have one gridview in that i have two coloums one bounded field another one is link button.. if i click link button the related bounded field text should be copied to another page

XML
<asp:GridView ID="GridView1" runat="server" ShowHeader="False"
                   AutoGenerateColumns="false" onrowcommand="GridView1_RowCommand">
               <Columns>
               <asp:BoundField DataField="Address" />
                <asp:TemplateField HeaderText="Jobs">
               <ItemTemplate>
               <asp:LinkButton ID="Update" runat="server" CommandName="cmdid" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>">Update</asp:LinkButton>
               </ItemTemplate>
           </asp:TemplateField>
               </Columns>
               </asp:GridView>


XML
public void Gridbind()
{
dt = new DataTable();

            dt.Columns.Add(new DataColumn("Address", typeof(string)));
            DataSet dl = Deliverydetails();
            int count = dl.Tables[0].Rows.Count;
            for(int i=0;i<count;i++)
            {
                DataRow dr = null;
                dr = dt.NewRow();
           string data = dl.Tables[0].Rows[i]["clad_name"].ToString() + ',' +.......;
                dr["Address"]=data;
                  dt.Rows.Add(dr);

            }
           GridView1.DataSource=dt.DefaultView;
            GridView1.DataBind();
}

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          if (e.CommandName == "cmdid")
          {
              int index = Convert.ToInt32(e.CommandArgument);

              GridViewRow selectrow = GridView1.Rows[index];

// here how to get value of selected rows Text
       
          }
Posted

Ty this way:
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.
     // store it and access in another page.
  }
}

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 and cast that into string.
 
Share this answer
 
C#
 if (e.CommandName == "cmdid")
          {
              int index = Convert.ToInt32(e.CommandArgument);
 
              GridViewRow selectrow = GridView1.Rows[index];
               string value=selectrow .Cells[0].Text;
                Response.Redirect("address.aspx?id="+value);
}



you can now get the querystring value and access it in the next page.

Hope this helps you
 
Share this answer
 
just Changed
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdid")
{
int index = Convert.ToInt32(e.CommandArgument);
string value = GridView1.Rows[index].Cells[1].Text.ToString();


Response.Redirect("address.aspx?id=" + value);

}
}
 
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