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
C# |
but label1 is null and maybe ProdID is null;
why?

Rujuta133 reputationPosted Nov 22, 2009, 10:52 PM EST
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 Jalali165 reputationPosted Nov 22, 2009, 10:17 PM EST
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
Rujuta133 reputationPosted Nov 22, 2009, 8:31 PM EST
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....
Rujuta133 reputationPosted Nov 22, 2009, 9:07 PM EST
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....
Rujuta133 reputationPosted Nov 22, 2009, 10:53 PM EST
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
}
}