2017-02-14 43 views
0
 <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)事件即時創建一個動態按鈕如何創建內部中繼和onclick事件

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 [email protected]"; 
    } 

我能夠創建動態按鈕,但btnq_Click事件不會觸發,我的按鈕正在消失。請建議我如何觸發事件&按鈕應該是可見的真實始終即使在事後回溯法

+1

動態每次加載頁面時都必須重新創建控件。一旦選擇被點擊,你正在創建一個按鈕,但是當你在回發上點擊你的動態按鈕時,你不會重新創建它。爲什麼不從一開始就創建一個不可見的按鈕,並改變其可見性? – Andrei

+0

謝謝安德烈您的迴應..我無法創建隱形按鈕。因爲如果一個或2個按鈕意味着我可以製作不可見的按鈕,但它會是n個數字按鈕,它可能是1或它可能是10個按鈕或任何東西..基於我的數據集,只有我會了解 –

回答

0

試試這個:

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