更新链接按钮未触发

时间:2019-10-23 12:00:05

标签: c# asp.net sql-update

我正在构建Web应用程序。我遇到了“更新链接”按钮无法触发的问题。我已经测试了到数据库的连接,并且插入语句(尽管下面未显示链接按钮)在我创建的每种表单上均能正常工作。我自己和我的同事无法弄清为什么“更新链接”按钮不更新数据库。

<asp:Content runat="server" ID="Program" ContentPlaceHolderId="MainContent">
    <asp:UpdatePanel ID="ProgramUpdatePanel" runat="server">
        <ContentTemplate>

        <asp:GridView ID="gridView1" runat="server" AllowSorting="true" AutoGenerateColumns="False" DataKeyNames="PRIMARY_KEY_ID" DataSourceID="SqlDataSource2" OnRowCancelingEdit="gridView1_RowCancelingEdit" OnRowEditing="gridView1_RowEditing" OnRowUpdating="gridView1_RowUpdating" OnRowUpdated="gridView1_RowUpdated">
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                        <asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="PRIMARY_KEY_ID" HeaderText="PRIMARY_KEY_ID" InsertVisible="False" ReadOnly="True" SortExpression="PRIMARY_KEY_ID" />
                <asp:TemplateField HeaderText="PROGRAM" SortExpression="PROGRAM">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtProgram" runat="server" Text='<%# Bind("PROGRAM") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblProgram" runat="server" Text='<%# Bind("PROGRAM") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="DESC" SortExpression="DESC">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDesc" runat="server" Text='<%# Bind("DESC") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblDesc" runat="server" Text='<%# Bind("DESC") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="RANK" SortExpression="RANK">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtRank" runat="server" Text='<%# Bind("RANK") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblRank" runat="server" Text='<%# Bind("RANK") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ACTIVITY" SortExpression="ACTIVITY">
                    <EditItemTemplate>
                        <asp:CheckBox ID="cbActivityEdit" runat="server" Checked='<%# Bind("ACTIVITY") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbActivity" runat="server" Checked='<%# Bind("ACTIVITY") %>' Enabled="false" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DATE_ORIGIN" SortExpression="DATE_ORIGIN">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDateOrigin" runat="server" Text='<%# Bind("DATE_ORIGIN") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblDateOrigin" runat="server" Text='<%# Bind("DATE_ORIGIN") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DATE_MODIFIED" SortExpression="DATE_MODIFIED">
                    <EditItemTemplate>
                        <asp:Label ID="lblDateModifiedEdit" runat="server" Text='<%# Bind("DATE_MODIFIED") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblDateModified" runat="server" Text='<%# Bind("DATE_MODIFIED") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ORIGIN_BY" SortExpression="ORIGIN_BY">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtOriginBy" runat="server" Text='<%# Bind("ORIGIN_BY") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblOriginBy" runat="server" Text='<%# Bind("ORIGIN_BY") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="MODIFIED_BY" SortExpression="MODIFIED_BY">
                    <EditItemTemplate>
                        <asp:Label ID="lblModifiedByEdit" runat="server" Text='<%# Session["CurrentUserName"] %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblModifiedBy" runat="server" Text='<%# Bind("MODIFIED_BY") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="HISTORY" SortExpression="HISTORY">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtHistory" runat="server" Text='<%# Bind("HISTORY") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblHistory" runat="server" Text='<%# Bind("HISTORY") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>
    <div>
    </div>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DB_CONNECTION %>" 
            SelectCommand="SELECT * FROM [tblPRIMARY]"
            InsertCommand="INSERT INTO [tblPRIMARY]
            ([PROGRAM],[DESC], [RANK], [ACTIVITY], [DATE_ORIGIN], [DATE_MODIFIED], [ORIGIN_BY], [MODIFIED_BY], [HISTORY])
            VALUES
            (@PROGRAM, @DESC, @RANK, @ACTIVITY, @DATE_ORIGIN, @DATE_MODIFIED, @ORIGIN_BY, @MODIFIED_BY, @HISTORY)"
            UpdateCommand="UPDATE [tblPRIMARY]
            SET [PROGRAM] = @PROGRAM,
            [DESC] = @DESC,
            [RANK] = @RANK,
            [ACTIVITY] = @ACTIVITY,
            [DATE_ORIGIN] =@DATE_ORIGIN,
            [DATE_MODIFIED] = @DATE_MODIFIED,
            [ORIGIN_BY] = @ORIGIN_BY,
            [MODIFIED_BY] = @MODIFIED_BY,
            [HISTORY] = @HISTORY
            WHERE [PRIMARY_KEY_ID] = @PRIMARY_KEY_ID"
            >
            <InsertParameters>
                <asp:Parameter Name="PRIMARY_KEY_ID" />
                <asp:Parameter Name="PROGRAM" Type="String" />
                <asp:Parameter Name="DESC" Type="String" />
                <asp:Parameter Name="RANK" Type="String" />
                <asp:Parameter Name="ACTIVITY" Type="Boolean" />
                <asp:Parameter Name="DATE_ORIGIN" Type="DateTime" />
                <asp:Parameter Name="DATE_MODIFIED" Type="DateTime" />
                <asp:Parameter Name="ORIGIN_BY" Type="String" />
                <asp:Parameter Name="MODIFIED_BY" Type="String" />
                <asp:Parameter Name="HISTORY" Type="String" />
            </InsertParameters>

            <UpdateParameters>
                <asp:Parameter Name="PRIMARY_KEY_ID" />
                <asp:Parameter Name="PROGRAM" Type="String" />
                <asp:Parameter Name="DESC" Type="String" />
                <asp:Parameter Name="RANK" Type="String" />
                <asp:Parameter Name="ACTIVITY" Type="Boolean" />
                <asp:Parameter Name="DATE_ORIGIN" Type="DateTime" />
                <asp:Parameter Name="DATE_MODIFIED" Type="DateTime" />
                <asp:Parameter Name="ORIGIN_BY" Type="String" />
                <asp:Parameter Name="MODIFIED_BY" Type="String" />
                <asp:Parameter Name="HISTORY" Type="String" />
            </UpdateParameters>
             </asp:SqlDataSource>

            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Content>

任何帮助将不胜感激!谢谢。

1 个答案:

答案 0 :(得分:0)

似乎您正在页面中使用“验证”控件。您的页面必须先通过验证,然后才能回发到服务器。下面是CausesValidation属性的功能:

  • CausesValidation属性指定在以下情况下是否验证页面 单击按钮控件。

  • 默认情况下单击按钮时将执行页面验证。

  • 此属性主要用于在单击取消或重置按钮时阻止验证。

尝试在CausesValidation="False"按钮中设置Update