命令按钮永远不会被击中

时间:2016-01-25 19:05:56

标签: c# asp.net linkbutton

我的e.CommandName行有一个断点,但这一行永远不会被击中。我还尝试添加一个写入浏览器的catch块,但我只是收到“页面无法显示”的错误,当出现问题时抛出典型的Chrome错误,与我的项目无关。以下是我的代码,是什么阻止我的e.CommandName行被击中?

C#Page

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack) { LoadGrid(); }
}

protected void LoadGrid()
{   
    this.datagrid12.Visible = false;
    this.datagrid12.Visible = true;
    _dataSet = SQLQueryToPullInData
    this.datagrid12.DataSource = _dataSet;
    this.datagrid12.DataBind();
}

Global.asax中

void Application_Error(object sender, EventArgs e)
{ 
  Exception CurrentException = Server.GetLastError();
  Server.ClearError();
if (CurrentException != null)
{
    try
    {
        using (StreamWriter sw = new StreamWriter(Server.MapPath("ErrorLog.txt")))
        {
            sw.WriteLine(CurrentException.ToString());
            sw.Close();
        }
    }
    catch (Exception ex) { }
  }
}

HTML

<asp:DataGrid runat="server" ID="datagrid12" AutoGenerateColumns="false" ShowFooter="true" OnItemCommand="datagrid12_ItemCommand" >
<Columns>
<asp:TemplateColumn HeaderText="ID" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="40" Visible="true">
    <ItemTemplate>
        <asp:LinkButton ForeColor="White" ID="btnEdit" runat="server" CausesValidation="True" CommandName="Edit" Text='<%# Eval("uID") %>' CommandArgument='<%# Eval("uID") %>'>
        </asp:LinkButton>
    </ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="uID" HeaderText="uID" Visible="false"></asp:BoundColumn>
<asp:BoundColumn DataField="name" HeaderText="Name"></asp:BoundColumn>

从Global.Asax写入文件的错误

  

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Web.HttpException (0x80004005): Maximum request length exceeded.
   at System.Web.HttpRequest.GetEntireRawContent()
   at System.Web.HttpRequest.FillInFormCollection()
   at System.Web.HttpRequest.EnsureForm()
   at System.Web.HttpRequest.get_Form()
   at System.Web.HttpRequest.get_HasForm()
   at System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull)
   at System.Web.UI.Page.DeterminePostBackMode()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.pages_usersreport_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\cfdef70b\4f768444\App_Web_0tprbakj.1.cs:line 0
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

我试过的其他代码

protected void datagrid12_ItemCommand(object source, DataGridCommandEventArgs e)
    {
    try
    {
        if (e.CommandName == "Edit")
        {
            LinkButton btn = ((LinkButton)e.Item.FindControl("btnEdit"));
            string uid = btn.CommandArgument;
        }
    }
    catch (Exception exception) { throw exception; }
    }

修改

我更新了我的html以添加此

OnClick="LinkButton_Click"

并添加了命令事件以显示:

protected void LinkButton_Click(Object sender, EventArgs e)
{
  string commandArgument = (sender as LinkButton).CommandArgument;
  string commandName = (sender as LinkButton).CommandName;
  if (commandName == "Edit")
  {
     Response.Redirect("../page2.aspx", false);
  }
}

但是在这个例子中,string commandName上的断点也没有被击中。

如果我查看控制台输出,当我的页面无法加载时,这就是我所看到的,对我来说没有任何描述,但可能对另一个有意义。

  

POST http://localhost:1234/page2.aspx net :: ERR_Connection_Reset

关于TimeOut的编辑
我的查询设置为不超时(或者我认为),我的MaxHttpCollectionKeys也设置为非常高的数字,以便不超时。查询运行正常,并按原样显示结果。问题在于我从显示的网格中按下链接按钮。

SqlCommand.CommandTimeout = 0;

<add key="aspnet:MaxHttpCollectionKeys" value="20000"/>

要修复
为了解决这个问题,我必须将以下2行添加到我的web.config文件

<system.web>
  <httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
</system.web>

<system.webServer>
  <security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
</system.webServer>

1 个答案:

答案 0 :(得分:1)

如果我的aspx格式不正确(忘记结束标记等),我通常会遇到这种类型的错误。确认您的网格具有正确的结束标记。

此外,您是否可以显示您的Page_Load事件代码以及如何填充网格?

最后,在您的global.asax.cs中,添加以下代码行:

void Application_Error(object sender, EventArgs e)
{
    try
    {
        Exception exception = Server.GetLastError();
        Server.ClearError();
        if(exception != null)
        {
          // Do Some Stuff!
        }
    }
    catch(Exception ex)
    {
    }
}

在Server.ClearError()中添加一个断点;线。这应该允许您进一步查看引发的错误。