开发者

how can i store value in button at runtime?

I have a problem i have some dynamic button.and i want to store integer value in that.

and get that value on that click event of that button how can i achieve it

thanks in advance shashank

DataView dv =开发者_如何学JAVA new DataView(dtCat, "PK_CATEGORY_ID IN(" + categoryIds.ToString() + "0)", "PK_CATEGORY_ID", DataViewRowState.CurrentRows);

foreach (DataRowView rr in dv)
{
    //MessageBox.Show(rr[0].ToString() + "------------" + rr[1].ToString());

    Button b2 = new Button();

    //b2.Name = rr[0].ToString();
    b2.Name = "";
    b2.Height = 200;
    b2.Width = 200;
    b2.Margin = new Thickness(0, -100, 0, 0);
    b2.HorizontalAlignment = HorizontalAlignment.Left;
    b2.Content = rr[1].ToString();
    b2.Background = System.Windows.Media.Brushes.Pink;
    b2.Click += new RoutedEventHandler(b2_Click);
    btncanvas.Children.Add(b2);
    Canvas.SetLeft(b2, b2.Width * i);
    i = i + 1;
    MessageBox.Show(rr[0].ToString());
    b2.Tag = rr[0].ToString();

}


void b2_Click(object sender, RoutedEventArgs e)
{
    Button clicked = (Button)sender;

    categoryname = clicked.Name;
}


The Tag property is probably what you want.

You are already using it in your example, but just have:

b2.Tag = integerValue;

Then in your click handler use Convert.ToInt32(object) method to get the integer value back:

int retrievedValue = Convert.ToInt32(clicked.Tag);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜