在ASP.NET DataGrid的EditCommandColumn中单击“更新”不会启动在codeehind中定义的
本文关键字:定义 codeehind 启动 单击 NET ASP DataGrid EditCommandColumn 更新 | 更新日期: 2023-09-27 18:30:00
我有一个DataGrid,它从实体数据模型中的几个表接收数据。我使用EditCommandColumn来提供数据编辑,以及绑定到数据库上存储过程中函数导入列的TemplateColumns。
以下是aspx代码中用于创建asp:DataGrid的部分
<asp:UpdatePanel ID="gridUpdate" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div style="vertical-align: top; height:250px; overflow:auto; width:1800px;">
<asp:DataGrid ID="dgdEditQ" runat="server" AllowPaging="True" AllowSorting="True"
BackColor="AntiqueWhite" BorderColor="Green" BorderStyle="Ridge"
CellPadding="10" Font-Bold="True"
Font-Size="Large" Width="1800px" Height="250px" OnEditCommand="dgdEditQ_Edit"
OnCancelCommand="dgdEditQ_Cancel" OnUpdateCommand="dgdEditQ_Update"
CellSpacing="10" ViewStateMode="Disabled" ItemStyle-Wrap="False"
ItemStyle-Width="100" AutoGenerateColumns="False">
<AlternatingItemStyle />
<Columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
HeaderText="Edit item"
ButtonType="LinkButton">
</asp:EditCommandColumn>
<asp:TemplateColumn Visible="true">
<HeaderTemplate>
<b> Quote Number </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("QuoteNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="false">
<HeaderTemplate>
<b> Name </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblEdN" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Eval("Name") %>' ID="txbEdName" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txbEdName" ErrorMessage="The name of the quote is required."></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="false">
<HeaderTemplate>
<b> Street </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblEdSt" runat="server" Text='<%#Eval("Street") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Eval("Street") %>' ID="txbEdStreet" MaxLength="50"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="false">
<HeaderTemplate>
<b> City & State </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblEdCS" runat="server" Text='<%#Eval("CityState") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Eval("CityState") %>' ID="txbEdCS" MaxLength="50"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<SelectedItemStyle />
</asp:DataGrid>
</div>
</ContentTemplate>
</asp:UpdatePanel>
当我在每次都有效的EditCommandColumn中单击Edit时,Cancel也可以,但Update按钮的单击从未触发我代码后面的onclick事件处理程序:
protected void dgdEditQ_Update(Object sender, DataGridCommandEventArgs e)
{
DataGridItem dgi = dgdEditQ.SelectedItem;
TextBox[] myBoxes = new TextBox[26];
string[] myParams = new string[26];
myBoxes[0] = (TextBox)dgi.FindControl("Quote Number");
myBoxes[1] = (TextBox)dgi.FindControl("Name");
myBoxes[2] = (TextBox)dgi.FindControl("Street");
myBoxes[3] = (TextBox)dgi.FindControl("City & State");
myBoxes[4] = (TextBox)dgi.FindControl("Type of Quote");
myBoxes[5] = (TextBox)dgi.FindControl("List Provided By");
myBoxes[6] = (TextBox)dgi.FindControl("Estimator");
myBoxes[7] = (TextBox)dgi.FindControl("Date Received");
myBoxes[8] = (TextBox)dgi.FindControl("Date Due");
myBoxes[9] = (TextBox)dgi.FindControl("Date of Plans");
myBoxes[10] = (TextBox)dgi.FindControl("Date of Revision");
myBoxes[11] = (TextBox)dgi.FindControl("Revision #");
myBoxes[12] = (TextBox)dgi.FindControl("Plan Name");
myBoxes[13] = (TextBox)dgi.FindControl("Customer");
myBoxes[14] = (TextBox)dgi.FindControl("Amount");
myBoxes[15] = (TextBox)dgi.FindControl("Quote Status");
myBoxes[16] = (TextBox)dgi.FindControl("Excel File");
myBoxes[17] = (TextBox)dgi.FindControl("Folder Location");
myBoxes[18] = (TextBox)dgi.FindControl("Architect");
myBoxes[19] = (TextBox)dgi.FindControl("Architectural Firm");
myBoxes[20] = (TextBox)dgi.FindControl("Architect's Phone");
myBoxes[21] = (TextBox)dgi.FindControl("Architect's Fax");
myBoxes[22] = (TextBox)dgi.FindControl("Engineer");
myBoxes[23] = (TextBox)dgi.FindControl("Engineering Firm");
myBoxes[24] = (TextBox)dgi.FindControl("Engineer's Phone");
myBoxes[25] = (TextBox)dgi.FindControl("Engineer's Fax");
for (int j = 0; j < 26; j++)
{
myParams[j] = myBoxes[j].Text;
}
}
我是ASP.NET的新手,但迄今为止我所做的所有研究都没有为我提供答案,解释为什么当我单击"更新"按钮时,我的"更新"事件没有启动。
即使您的事件处理程序正在启动,它实际上也不会更新任何内容,因为您所做的只是将每个文本框的文本值存储到字符串数组中,然后不将其保存在任何位置,也不更新UI(即重新绑定网格)。
您需要在处理程序中具有类似的逻辑:
for (int j = 0; j < 26; j++)
{
myParams[j] = myBoxes[j].Text;
}
// Send myParams string array to some logic that will save it (i.e. database)
// Rebind the grid so that the changes will be reflected to the user once they exit edit mode
dgdEditQ.DataSource = GetDataFromDatabase();
dgdEditQ.DataBind();
我重现了您的问题,并发现它已被删除:ViewStateMode="Disabled"