hi friends
i have a datalist on my page and design it on template
i have some button and image and hyperlink to it;
now i wand when i clicked a button of one item's , i can get id of that item
how can i do that?
my code is here but with some error
protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ProdID.ToString(); // ShoppungCart.GetShoppingCart().AddItem(ProdID); // Response.Redirect("ViewCart.aspx"); } protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { int ProdID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]); }
|
but label1 is null and maybe ProdID is null;
why?
RujutaPosted Nov 23, 2009, 3:52 AM
Now in the coding
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
int i;
if (e.CommandName == "Add to Cart")
{
i = e.Item.ItemIndex;
Label l = (Label)DataList1.Items[i].FindControl("IdLabel");
Response.Write(l.Text);
//here add the logical implementation of adding item to cart now that you have got hold of your id
}
}
RujutaPosted Nov 23, 2009, 3:53 AM
Now in the coding
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
int i;
if (e.CommandName == "Add to Cart")
{
i = e.Item.ItemIndex;
Label l = (Label)DataList1.Items[i].FindControl("IdLabel");
Response.Write(l.Text);
//here add the logical implementation of adding item to cart now that you have got hold of your id
}
}
Bahar JalaliPosted Nov 23, 2009, 3:17 AM
i have a DATALIST no gridview
and my datalist is in a page that users can see it
and i don't want have any checkbox on it
really i want have an image button that work like select in a datalist
it's means when users click on imagebutton , that product added to the shopping cart
RujutaPosted Nov 23, 2009, 2:07 AM
Enable selecting in your gridview and then in code behind use the following code
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int i = GridView1.SelectedIndex;
Response.Write(GridView1.Rows[i].Cells[1].Text);
Response.Write(GridView1.Rows[i].Cells[2].Text);
//Response.Write(i);
}
Hope this helps too....
RujutaPosted Nov 23, 2009, 1:31 AM
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("cbxSelect")).Checked;
if (isChecked)
{
str.Append(GridView1.Rows[i].Cells[0].Text);
}
}
Label1.Text = str.ToString();
Hope this helps
Happy Coding....