如何在转发器和onclick事件中创建动态按钮

时间:2017-02-14 12:54:26

标签: c# asp.net repeater

      <asp:Repeater ID="rpt_title" runat="server"   >
                 <ItemTemplate>                         

                     <div class="col-md-12">
                         <div class="panel panel-default">
                             <div class="panel-body">

                                 <div class="col-md-10 col-sm-10">
                                     <div class="quesion-div">
                                         <div class="quesion-history">
                                             <asp:Label ID="Label2" runat="server"  Text='<%#Eval("category") %>' ></asp:Label>
                                         </div>
                                         <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("Node_Desciption") %>'></asp:LinkButton>
                                          <asp:Label ID="lbl_titleid" runat="server" Text='<%#Eval("Node_Id") %>' Visible="false"></asp:Label>
                                     </div>
                                 </div>
                                 <div class="col-md-2">
                                     <div class="question-button">
                                         <asp:Button ID="Button1" class="btn btn-primary btn-lg btn-block" runat="server" Text="Select" OnClick="lnk_Click" />
                                     </div>
                                 </div>
                             </div>
                         </div>
                     </div>
                 </ItemTemplate>
             </asp:Repeater>

当我点击选择按钮(onclick = lnk_Click)事件即时创建一个动态按钮

 protected void lnk_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds = new DataSet();
            Button btn = (Button)sender;
            RepeaterItem item = (RepeaterItem)btn.NamingContainer;
            Label lbid = (Label)item.FindControl("lbl_titleid");
            ent.ThirdLevelCategoryID = Convert.ToInt32(lbid.Text);
            ent.Task = "bind_question";
            ds = ser.bind_nodedescription_basedcatid(ent);
            rpt_question.DataSource = ds;
            rpt_question.DataBind();
            div_title.Visible = false;
            div_question.Visible = true;
            title_category.Visible = false;
            ptitle.Visible = false;

            Button btnquestion = new Button();
            btnquestion.Text = "Question";
            btnquestion.ID = "btnquestion";
            btnquestion.CssClass = "btn btn-primary";
            btnquestion.OnClientClick += new System.EventHandler(btnq_Click);
            btnquestion.Click += new System.EventHandler(btnq_Click);
            this.form1.Controls.Add(btnquestion);


        }
        catch (Exception ex)
        {

            ErrorLoggermain.logError(ErrorLoggermain.enumErrorTypes.AppLogicError, ex, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, "12");
        }
    }

    protected void btnq_Click(object sender, EventArgs e)
    {
        lbl_test.Text = "The email id for the customer is Goyal2@yahoo.com";
    }

我能够创建动态按钮,但btnq_Click事件未触发,我的按钮正在消失。请建议我如何解雇事件&amp;即使在后置方法

中,按钮应始终可见

1 个答案:

答案 0 :(得分:0)

试试这个:

protected void lnk_Click(object sender, EventArgs e)
{
    Button btn = (Button) sender;
    string buttonText = btn.Text;
}

- 服务器端代码

function findNode(nodes, prop, value) {
  if(!value || !(nodes instanceof Array)) return;
  for(var i=0; i<nodes.length; i++) {
      if(node = (value == nodes[i][prop]) ? nodes[i] : findNode(nodes[i]['nodes'], value)) {
        return node;
      }
  }
}