【问题标题】:how to Create dynamic button inside repeater and onclick event如何在中继器和 onclick 事件中创建动态按钮
【发布时间】:2017-02-14 22:29:23
【问题描述】:
      <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 事件没有触发,我的按钮正在消失。请建议我如何触发事件和按钮应该始终可见,即使在回发方法中也是如此

【问题讨论】:

  • 每次加载页面时都必须重新创建动态控件。单击 Select 后,您正在创建一个按钮,但是当您单击回发上的动态按钮时,您并没有重新创建它。为什么不从一开始就创建一个不可见的按钮并更改其可见性?
  • 感谢 Andrei 的回复。我无法创建隐形按钮。因为如果一个或 2 个按钮意味着我可以制作隐形按钮,但它将是 n 个数字按钮,它可能是 1 或可能是 10 个按钮或任何东西.. 基于我的数据集,我只会了解

标签: c# asp.net repeater


【解决方案1】:

试试这个:

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

-- 服务器端代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 2020-03-24
    • 1970-01-01
    • 2012-08-17
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多