1

我正在尝试学习 ASPX 并且正在做一个网站(学习目的)

当单击列表中的名称时,我在网格视图中有一个员工列表我必须将值传递给 db 并在单独的页面中显示详细信息。我通过使用这段代码来做到这一点

LinkButton lnkCustomerID = sender as LinkButton;
string strCustomerID = lnkCustomerID.CommandArgument;
Response.Redirect("EmployeeDetail.aspx?Parameter=" + strCustomerID);

它工作得很好。

在详细信息页面中,我在另一个网格中显示详细信息,我使用下面的代码进行了此操作

 SqlCommand cmdCustomerDetails = new SqlCommand("GetEmployee", sqlConnect);
             cmdCustomerDetails.CommandType = CommandType.StoredProcedure;
             cmdCustomerDetails.Parameters.AddWithValue("@ID", employeeId);
             if (sqlConnect.State == ConnectionState.Closed)
                 sqlConnect.Open();
             //Create DataReader to read the record
             SqlDataReader dReader = cmdCustomerDetails.ExecuteReader();
             GridView2.DataSource = dReader;
             GridView2.DataBind();
             sqlConnect.Close();

它也有效,但是当我尝试通过单击作为项目模板的更新按钮来更新值时,如图所示

<asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("name")%>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rqdfldName" runat="server" ErrorMessage="Name Cannot Be Empty"
                        ViewStateMode="Disabled" ControlToValidate="txtName" ValidationGroup="Update"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Age">
                <ItemTemplate>
                    <asp:TextBox ID="txtAge" runat="server" Text='<%# Eval("age")%>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Address">
                <ItemTemplate>
                    <asp:TextBox TextMode="MultiLine" ID="txtAddress" runat="server" Text='<%# Eval("address")%>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnUpdate" runat="server" Text="Update" ValidationGroup="Update"
                        CommandArgument='<%#Eval("id") %>' OnClick="btnUpdate_Click" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>

我收到此错误

无效的回发或回调参数。使用配置或页面中的 <%@ Page EnableEventValidation="true" %> 启用事件验证。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证。

谁能帮帮我

我可以通过像这样将该页面的属性设置为 false 来克服该错误

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" EnableEventValidation="false" AutoEventWireup="true" CodeFile="EmployeeDetail.aspx.cs" Inherits="EmployeeDetail" %>

但是当我单击更新按钮并找到控件的值时,我没有得到更新的值

int empID;
        string age, address, name;
        Button btnUpdateUser = sender as Button;
        TextBox tbName, tbAge, tbAddress;
        SqlConnection sqlConnect;
        try
        {
            tbName = (TextBox)GridView2.Rows[0].FindControl("txtName");
            tbAge = (TextBox)GridView2.Rows[0].FindControl("txtAge");
            tbAddress = (TextBox)GridView2.Rows[0].FindControl("txtAddress");
            sqlConnect = new SqlConnection("server=WINNER-PC\\SQLEXPRESS;integrated security=true;initial catalog=Test");
            if (sqlConnect.State == ConnectionState.Closed)
                sqlConnect.Open();
            empID = Convert.ToInt32(btnUpdateUser.CommandArgument);
            name = tbName.Text;
            age = tbAge.Text;
            address = tbAddress.Text;
            SqlCommand sqlCommand = new SqlCommand("UpdateEmployee", sqlConnect);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("@ID", empID);
            sqlCommand.Parameters.AddWithValue("@Name", name);
            sqlCommand.Parameters.AddWithValue("@Age", age);
            sqlCommand.Parameters.AddWithValue("@Address", address);
            sqlCommand.ExecuteNonQuery();
            Response.Redirect("Employees.aspx", false);
        }
        catch (Exception ex)
        {
            throw ex;
        }

当两个网格视图都在同一页面中时,我能够更新,但是当我将第二个网格移动到新页面时,更新不起作用..请帮帮我

4

0 回答 0