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

标签 c# asp.net datagrid

我有一个从实体数据模型中的多个表接收数据的 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/

相关文章:

c# - ASP.NET MVC 哈希符号在参数中被忽略

c# - 除非我先清理/重建解决方案,否则 Visual Studio 2012 不会应用更改

c# - 有没有一种方法可以将 DataRow 映射到类的对象

wpf - 在触发器上设置 Wpf AlternatingRowBackground 样式

javascript - 使用数据网格功能创建可移植的 HTML 报告

c# - 如何在 Entity Framework Core 3.1 中对自定义对象执行原始 SQL 查询,而不需要创建表的迁移?

C# Windows 注册表 GetValue 错误

asp.net - 如何在 Windows 和 .NET 上运行的 AWS ElasticBeanstalk 上自动安装 SSL 证书?

asp.net - _events为null或不是对象错误,仅当编译debug =“false”时

silverlight - 如何限制单元格的选择以排除 DataGrid 中的列?