hey, I m creating a small project shopping cart....In gridview. I want to add a Hyperlink to each row of GridvIew, & when the user click on Hyperlink(Add to cart),data go to database..

I add a hyperlink code,but the problem is that,it is not firing,To fire the event for hyperlink..I use RowDataBoundEvent..Mine code as below-

<Columns>
                                             
                        <asp:BoundField HeaderText="Product Name" DataField = "productname"/>
                            
                       <asp:TemplateField HeaderText="images">
                       <itemtemplate>
                       <asp:Image ID="Image1"  Width="70" Height="70"  runat="server" ImageAlign ="Middle" ImageUrl='<%#"GetDBImage.ashx?id=" + Eval("id")  %>' />
                       </itemtemplate>
                       </asp:TemplateField>
                
                      <asp:BoundField HeaderText="Price" DataField ="price" />
                          
                      <asp:TemplateField HeaderText= "Add To cart"  > 
                      <ItemTemplate> 
                      <asp:HyperLink ID="AddTocartHyperlink" runat ="server" Text ="Add To Cart" />
                      </ItemTemplate>
                      </asp:TemplateField>
               
                       </Columns>
public partial class AddProductsToCart : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=SQL_SERVER_NAME;Initial catalog=DBName;Integrated Security=true");

    protected void Page_Load(object sender, EventArgs e)
    {
        GridViewProducts.DataSource = FetchAllImagesInfo();
        GridViewProducts.DataBind();
    }

    public DataTable GetImages()
    {
        string sql = "Select * from Products";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
    
    protected void GridViewProducts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row .RowType == DataControlRowType .DataRow )
        {
                      string a = e.Row.Cells[0].Text;
            string b= e.Row.Cells[2].Text; 

        }

    }

hey first tell me that the event that i used RowDataBound,is the Correct event so that when the user suppose clicks on Add To cart of first row,only first row data ts retrievd...

On RowDataBound I just want to get the Column data,Mine above code is right or not???

HI,
I think no need to use data_bound. You can easily transfer an unique id to next page where you can retrieve the recordset. Just configure the hyperlink within gridview in the following way:

<ItemTemplate>
                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "abc.aspx?NavigateID="+Eval("ID") %>'>HyperLink</asp:HyperLink>
                </ItemTemplate>

You have to modify this above to suit to your project.

I used button Field..

SOURCE TAB-

<Columns>
                                             
                        <asp:BoundField HeaderText="Product Name" DataField = "productname"/>
                            
                       <asp:TemplateField HeaderText="images">
                       <itemtemplate>
                       <asp:Image ID="Image1"  Width="70" Height="70"  runat="server" ImageAlign ="Middle" ImageUrl='<%#"GetDBImage.ashx?id=" + Eval("id")  %>' />
                       </itemtemplate>
                       </asp:TemplateField>
                
                      <asp:BoundField HeaderText="Price" DataField ="price" />
                          
                                  
               <asp:ButtonField CommandName="AddToCart" Text="Add To Cart" ControlStyle-ForeColor ="Blue" ></asp:ButtonField>
               
                       </Columns>

I m now able to get cell data,but not dat cell data in which image is there, I want to know that is it possible to get image data of image that is in GridView Cell.............

below code gets the string data.....

protected void GridViewProducts_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName =="AddToCart")
        {
             int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridViewProducts.Rows[index];
            string a =row.Cells[0].Text ; 
        }
    }

No, you can't get get image data. you can add a column to put your image addresss. then keep the column invisible by css.

Modify your css file in below way

.hide
{
	display:none;
}
This article has been dead for over six months. Start a new discussion instead.