命令按钮永远不会被击中
本文关键字:按钮 永远 命令 | 更新日期: 2023-09-27 18:32:00
我的e.CommandName
行上有一个断点,但这行从未被命中。 我还尝试添加一个可以写入浏览器的 catch 块,但我只是收到一个错误"页面无法显示",这是出现问题时抛出的典型 Chrome 错误,与我的项目无关。 下面是我的代码,是什么阻止了我的e.CommandName
线被击中?
C# 页
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();
}
全球阿萨克
斯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
上的断点。
如果我查看控制台输出,当我的页面无法加载时,这就是我所看到的,对我来说没有什么描述性的,但可能对另一个人有意义。
发布 http://localhost:1234/page2.aspx 网::ERR_Connection_Reset
编辑关于超时
我将查询设置为不超时(或者我认为是这样),并且我的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>
如果我的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() 行中放置一个断点。这应该可以让您进一步了解引发的错误。