0

我认为这个问题已经被问过很多次了(我自己问过一次),但是我面临一个新问题。我正在创建一个按钮数组,点击我的应用程序中的另一个按钮。创建的按钮数量取决于我从数据库获得的值和从数据库获得的值取决于我在查询中传递的会话值。我的代码如下..

代码

   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";

}

上面的方法attributes()将在按钮单击时被调用,并且会话值也会在按钮单击时生成。经过研究,我知道动态按钮的创建应该在 page_init 事件中完成,但我不能在这里做。请帮忙解决这个问题..

4

2 回答 2

1

看看这里。

http://blog.krisvandermast.com/AddingADynamicControlToAPlaceholderControlAndWireUpTheEvent.aspx

事件在那里,你只需要再次连接它。

于 2013-04-01T06:36:55.003 回答
0

您可以通过使用 jQuery 来实现这一点。
添加对网页的 jQuery 引用
获取适合类“标签”的所有控件,在其单击事件上调用服务器端的“lbl_click”方法。
确保“lbl_click”方法应标记为静态并使用 [webmethod] 进行修饰。

页面

<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
//Find all controls that belongs to class '.label'
//on its click event call the server side function.
  $(".label").click(function(e) {
    $.ajax({
      type: "POST",
      url: "Default.aspx/lbl_click",
      data: "{}", //can pass parameter here, if required
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      error:
      function(XMLHttpRequest, textStatus, errorThrown) {
        //Handle error here.
      },
      success:
      function(result) {
      //Set button css class to 'label1' using jQuery.
      }
    });
  });
});
</script>
</head>


代码背后

  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();
      }
    }
  } 



  [WebMethod]
  public static string lbl_click()
  {
    return "label1";
  }

希望这会帮助你。

于 2013-04-02T05:51:51.203 回答