对于按钮的排列按钮的Click事件不触发 [英] Button click event for array of buttons not firing

查看:158
本文介绍了对于按钮的排列按钮的Click事件不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题已经被问过很多次了(我我自己问一次),但对此我facing.I我在我的按钮application.Number创建按钮阵列的onclick另一个按钮的新问题创建将取决于价值这是我从数据库和价值得到这是我从数据库中获取取决于哪个我通过在query.My code是如下..会话值

I think this question have been asked many times already(I my self asked once),But there is a new problem which i am facing.I am creating an array of buttons onclick of another button in my application.Number of buttons created will depend upon values which i get from database and values which i get from database depends on a session value which i pass in the query.My code is as below..

code

   protected void attributes()
    {
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd = new SqlCommand("select attributesequenceNumber as attsno,ProductCode as pcode, P26 as '" + 26 + "',P28 as '" + 28 + "',P30 as '" + 30 + "',P32 as '" + 32 + "',P34 as '"+34+"',P36 as '"+36+"',P38 as '"+38+"',P40 as '"+40+"',P42 as '"+42+"',SHXS as XS,SHS as S,SHM as M,SHL as L,SHXL as XL,SHXXL as XXL from tblattribute where ProductCode='" + Session["ImgProdCode"] + "'", con);
    //SqlCommand cmd = new SqlCommand("select Col.value('local-name(.)', 'varchar(Max)') as ColName from (select * from tblattribute where ProductCode ='"+Session["ImgProdCode"]+"' for xml path(''), type) as T(XMLCol) cross apply T.XMLCol.nodes('*') as n(Col) where Col.value('.', 'varchar(1)') = 1 " , con);
    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        DataTable dtble = new DataTable();
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        dap.Fill(dtble);
        if (dtble.Rows.Count > 0)
        {
           result = dtble.Columns.Cast<DataColumn>()
        .Where(c => c.ColumnName != "pcode" && c.ColumnName != "attsno")
        .Where(c => dtble.Rows[0][c].ToString() == "1")
        .Select(c => c.ColumnName)
        .ToList();
            res = result.Count;
            lbl = new Button[res];
            for(i=0; i<result.Count; i++)
            {

                lbl[i] = new Button();
                lbl[i].Text = result[i];
                lbl[i].ID = "btn" + i.ToString();
                lbl[i].Width = 30;
                lbl[i].Click+=new EventHandler(lbl_click);
                lbl[i].CssClass = "label";
                div1.Controls.Add(lbl[i]);
            }

        }

    }
    catch
    {
        throw;
    }
    finally
    {
        if (con != null)
        {
            con.Close();
        }
    }

} 

protected void lbl_click(object sender, EventArgs e)
{

    Button lbl = sender as Button;
    lbl.CssClass = "label1";

}

以上方法的属性()将被要求按一下按钮,会话值也将buttonclick.On研究产生我才知道,动态按钮的创建应该做的事在page_init事件,但我不能做到这一点here.Please有助于解决这个问题。

The above method attributes() will be called on a button click,and the session value will also be generated on buttonclick.On research i came to know that the creation of dynamic buttons should be done in page_init event but i cannot do it here.Please help to solve this issue..

推荐答案

在这里看看。

<一个href=\"http://blog.krisvandermast.com/AddingADynamicControlToAPlaceholderControlAndWireUpTheEvent.aspx\" rel=\"nofollow\">http://blog.krisvandermast.com/AddingADynamicControlToAPlaceholderControlAndWireUpTheEvent.aspx

本次活动是有你只需要再次连线起来。

The event is there you just need to wire it up again.

这篇关于对于按钮的排列按钮的Click事件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆