2

我有一个 DataGrid,它从实体数据模型中的几个表中接收数据。我使用 EditCommandColumn 来提供数据编辑,以及从数据库上的存储过程绑定到函数导入列的 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 中单击 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 的新手,但到目前为止我所做的所有研究都没有为我提供答案,说明为什么单击 Update 按钮时我的 Update 事件没有触发。

4

2 回答 2

1

即使您的事件处理程序正在触发,它实际上也不会更新任何内容,因为您所做的只是将每个文本框的文本值存储到一个字符串数组中,然后不将其保存在任何地方,也不会更新 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();
于 2013-09-03T17:45:46.490 回答
0

我重现了您的问题,我发现它已删除: ViewStateMode="Disabled"

于 2013-09-03T17:50:43.010 回答