简体   繁体   English

为什么我的服务器端代码没有在 ASP.net 按钮点击时触发?

[英]Why my server side code is not firing on ASP.net Button click?

I don't understand why my server side is not executed.我不明白为什么我的服务器端没有执行。

Here is my code这是我的代码

ASP code ASP代码

<div class="row" style="padding-top:20px;">
    <div class="col-lg-4">
        <input id="btnSave" type="button" class="btn btn-info"  value="Save" />
        <input id="btnLoad" type="button" class="btn btn-info"  value="Load" />

         <%-- SAVE DIALOG  --%>
         <div id="saveDialog" title="Basic dialog">
         <div class="row">
             <div class="col-lg-4">
                 <asp:Label ID="lblFileName" CssClass="control-label" runat="server" Text="File Name"></asp:Label>
             </div>
             <div class="col-lg-6">
                 <asp:TextBox ID="txtFileName" CssClass="form-control" runat="server"></asp:TextBox>
             </div>
         </div>
         <div class="row">
             <div class="col-lg-12" style="text-align:center;">
                 <asp:Label ID="lblSaveErrorMsg" runat="server" Text="" ForeColor="Red"></asp:Label>
             </div>
         </div>
         <div class="row" style="padding-top:10px;">
             <div class="col-lg-12" style="text-align:center;">
                <asp:Button ID="btnSaveFile" runat="server" Text="Save" OnClick="btnSaveFile_Click"/>
                                
             </div>
         </div>
     </div>                   
</div>

Server Side Code服务器端代码

protected void btnSaveFile_Click(object sender, EventArgs e)
    {
        string cs = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString;

        using (SqlConnection con = new SqlConnection(cs))
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(ASPxPivotGrid1.SaveLayoutToString());
            MemoryStream stream = new MemoryStream(byteArray);

            DateTime currentDate = DateTime.Now;

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "INSERT INTO ReportSave(UserID,ReportName,UserFileName,ReportData,Time,ReportFilter)VALUES(@UserID,@ReportName,@UserFileName,@ReportData,@Time,@ReportFilter);";
            
            cmd.Parameters.AddWithValue("@UserID", UserID);
            cmd.Parameters.AddWithValue("@ReportName", ReportName);
            cmd.Parameters.AddWithValue("@UserFileName", txtFileName.Text);
            cmd.Parameters.AddWithValue("@ReportData", stream);
            cmd.Parameters.AddWithValue("@Time", currentDate);
            cmd.Parameters.AddWithValue("@ReportFilter", txtFilterRecord.Text);

            con.Open();
            cmd.ExecuteNonQuery();

            //string message = "Your details have been saved successfully.";
            //string script = "window.onload = function(){ alert('";
            //script += message;
            //script += "')};";
            //ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
        }
    }

Note:笔记:

It doesn't show any error message and it is not reloading the page as well.它不会显示任何错误消息,也不会重新加载页面。 It was weird.这很奇怪。

I call jquery functions, after it execute also it never call the server code.我调用 jquery 函数,在它执行之后它也从不调用服务器代码。 For testing purpose, I removed the entire jquery code for this button.出于测试目的,我删除了此按钮的整个 jquery 代码。 So no Jquery function for this button now.所以现在这个按钮没有 Jquery 功能。 But still I couldn't call the server side code why???但我仍然无法调用服务器端代码,为什么???

UPDATED更新

I found something, I have one button on the form called btnSave , once user click it will call我发现了一些东西,我在表单上有一个名为btnSave按钮,一旦用户单击它就会调用

$('#btnSave').click(function () {
    $('#saveDialog').dialog();
});

IMPORTANT NOTE: Then Dialog box will appear.重要提示:然后会出现对话框。 In that dialog box I have added that button btnSaveFile .在那个对话框中,我添加了那个按钮btnSaveFile But When I add this button outside the dialog, it's calling the server side code.但是当我在对话框外添加这个按钮时,它正在调用服务器端代码。

Hey friend protected void btnSaveFile_Click(object sender, EventArgs e) copied this method from other page or application then it will not work, So delete the event and event name assigned to the button.嘿朋友protected void btnSaveFile_Click(object sender, EventArgs e)从其他页面或应用程序复制了这个方法然后它不会工作,所以删除分配给按钮的事件和事件名称。 Go to design and go to button even properties go to OnClick event double click it, It will generate event and automatically assigns event name to the button.转到设计并转到按钮甚至属性转到 OnClick 事件双击它,它将生成事件并自动为按钮分配事件名称。 It will work.它会起作用。

Or或者

Give the new Onclick function name in the code在代码中给出新的 Onclick 函数名称

<asp:Button ID="btnSaveFile" runat="server" Text="Save" OnClick="btnSaveFile_Click"/>

Use full Links:使用完整链接:

asp.net Button OnClick event not firing asp.net Button OnClick 事件未触发

ASP.NET button not firing on click event ASP.NET 按钮不会在单击事件上触发

OnClick events not working in ASP.NET page OnClick 事件在 ASP.NET 页面中不起作用

https://www.codeproject.com/Questions/681648/button-click-event-not-firing-asp-net https://www.codeproject.com/Questions/681648/button-click-event-not-firing-asp-net

You may use this code in Jquery:您可以在 Jquery 中使用此代码:

$(document).on('click', '#btnSave', function() {
{
   $('#saveDialog').dialog();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM