gpt4 book ai didi

c# - 单击 ASP.NET DataGrid 的 EditCommandColumn 中的更新不会启动代码隐藏中定义的 Click 事件

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:23 25 4
gpt4 key购买 nike

我有一个从实体数据模型中的多个表接收数据的 DataGrid。我使用 EditCommandColumn 提供数据编辑以及绑定(bind)到从数据库上的存储过程导入的函数列的 TemplateColumns .

这是用于创建 asp:DataGrid 的 .aspx 代码部分

<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 中单击“编辑”时,“取消”也是如此,但单击“更新”按钮永远不会触发我后面的代码中的 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 (即重新绑定(bind)网格)。

您需要在您的处理程序中具有与此类似的逻辑:

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();

关于c# - 单击 ASP.NET DataGrid 的 EditCommandColumn 中的更新不会启动代码隐藏中定义的 Click 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18597935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com