2013-04-30 68 views
1

我觉得有点愚蠢,但我无法让我的按钮在我的用户控件中触发。我使用按钮创建了用户控件,并且在后面的代码中添加了事件处理程序。此用户控件引用已添加到父级,并且父级页面上有此用户控件的一个实例。用户控件按钮事件没有触发

当我点击按钮后,它会回传到上一页。我添加的断点不会受到影响。这可能很简单,我不做。

ASCX

<h2>Notes</h2> 

    <div id="divNoteForm"> 
     <h2>Create a new note</h2> 
     <br /> 
     <table> 
      <tr> 
       <td> 
        <asp:Label ID="lblTitle" runat="server" Text="Title"></asp:Label> 
       </td> 
       <td> 
        <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <asp:Label ID="lblNoteText" runat="server" Text="Note"></asp:Label> 
       </td> 
       <td> 
        <asp:TextBox ID="txtNoteText" runat="server" Width="300" Rows="2"></asp:TextBox> 
       </td> 
      </tr> 
      <tr> 
       <td> 

        <asp:Button ID="btnSubmit" runat="server" Text="Submit note" OnClick="btnSubmit_Click"/> 
       </td> 
      </tr> 
     </table> 
    </div> 

    <div id="divNoteGrid"> 
     <h2>Note Grid</h2> 
     <br /> 
     <table> 
      <tr> 
       <td>Date</td> 
       <td>Title</td> 
       <td>Description</td> 
      </tr> 
       <tr> 
       <td>22/04/2013</td> 
       <td>BSB</td> 
       <td>Phoned Amy Rowcliffe on 22/04/2013</td> 
       <td><u>Edit</u></td> 
        <td><u>Delete</u></td> 
      </tr> 
     </table> 
     </div> 


     <input type="radio" id="rdoForm" name="rdoNotes" style="width:10px" onchange="rdoChange(this)" value="Form" checked="checked">Show new note 
    <input type="radio" id="rdoGrid" name="rdoNotes" style="width:10px" onchange="rdoChange(this)" value="Grid">Show existing notes 
</div> 

<div id="NotesBtnForm" style="width: 500px; height: 40px; position: fixed; top: 0px; left:37%; border-style:solid; border-width:thin; z-index:10000; opacity:1.0; background-color:white" > 
    <asp:Button runat="server" ID="btnNotes" Text="Notes: 5" OnClientClick="showNotes();return false;" /> 
</div> 

按钮不会火btnSubmit按钮。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Model.Company; 
using Business; 

public partial class WebControls_DataWebControls_NotesControl : System.Web.UI.UserControl 
{ 

    CompanyNotesFactory companyNotesFormatter = new CompanyNotesFactory(); 
    List<CompanyNotesModel> results = new List<CompanyNotesModel>(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    public int NumToDisplay { get; set; } 
    public string Notes { get; set; } 
    protected string AdditionalInfo { get; private set; } 

    //public override void DataBind() 
    //{ 
    // // Process the data we have been passed 
    // ProcessNotes(Notes); 
    // base.DataBind(); 
    //} 

    public int NumNotes 
    { 
     get 
     { 
      return results.Count; 
     } 
    } 

    //private void ProcessNotes(string Notes) 
    //{ 
    // if (string.IsNullOrEmpty(Notes)) 
    // { 
    //  AdditionalInfo = "No Additional Information Available"; 
    //  return; 
    // } 

    // results = companyNotesFormatter.GetCompanyNotes(Notes); 

    // if (NumToDisplay == 0) 
    //  rptNotes.DataSource = results; 
    // else 
    //  rptNotes.DataSource = results.Take(NumToDisplay); 

    // AdditionalInfo = companyNotesFormatter.NotesLeadingText; 

    // rptNotes.DataBind(); 

    //} public void Add(string notesTitle, string notesText, Guid userID, int relatedID, string relatedType) 

    //public event EventHandler btnSubmit_Click; 

    protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     string test = Request["userid"]; 


     //Business.Core.Note.Add(txtTitle.Text, txtNoteText.Text, Request["userid"],); 
    } 

    } 
+1

您是否在UserControl之外有一些导致回传到上一页的逻辑?它是否回到上一页或重定向到它? – Netricity 2013-04-30 09:55:15

+0

我想你最有可能有一些无关的JS错误,因为ASP.NET无法进行回发。 – 2013-04-30 10:06:42

+0

@Intricity您的评论帮我弄明白了。谢谢! – 2013-04-30 11:02:49

回答

0

尝试使用圆括号“()”调用函数。

+1

这样做会给我一个运行时错误。如果将onclick事件处理程序添加到设计器中的按钮,则不应该有括号。你正在考虑一个JavaScript onclientclick这确实需要括号。 – 2013-04-30 09:20:31

0

我说得对,答案可能很明显。

该按钮的默认回传行为是到不同的URL。我正在开发的应用程序必须有一些配置,因为我以前使用过asp.net按钮,而且我不必手动设置URL。

感谢您的帮助。