我正在开发一个 asp.net 项目,我在 default.aspx 页面上有一个 gridview,数据绑定到我的 sql 数据库。当我单击特定行时,它会导航到 details.aspx 页面,您可以在其中查看和编辑所单击行的详细信息。
在该 details.aspx 页面上是另一个网格视图,我在其中公开了页脚并添加了“插入”链接按钮和每列的适当控件,以将新记录添加到数据库中。我已经对整个项目进行了编码,但不幸的是,当我单击“插入”链接按钮时没有任何反应(控件中的文本全部清除)。不会引发异常,但不会将记录添加到数据库中。这是网格视图代码:
<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" DataKeyNames="ClientId" DataSourceID="SqlDataSource1" ShowFooter="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="ClientId" InsertVisible="False" SortExpression="ClientId">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ClientId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ClientId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbInsert" runat="server" CommandName="insert" CausesValidation="false">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MiddleName" SortExpression="MiddleName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("MiddleName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("MiddleName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMiddleName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender" SortExpression="Gender">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Gender") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddGender" runat="server">
<asp:ListItem>M</asp:ListItem>
<asp:ListItem>F</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DateOfBirth" SortExpression="DateOfBirth">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("DateOfBirth") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("DateOfBirth") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ZipCode" SortExpression="ZipCode">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("ZipCode") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("ZipCode") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtZIP" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
这是代码隐藏:
Private Sub grid1_RowCommand1(sender As Object, e As GridViewCommandEventArgs) Handles grid1.RowCommand
If e.CommandName = "insert" Then
SqlDataSource1.InsertParameters("FirstName").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtFirstName"), TextBox).Text
SqlDataSource1.InsertParameters("MiddleName").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtMiddleName"), TextBox).Text
SqlDataSource1.InsertParameters("LastName").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtLastName"), TextBox).Text
SqlDataSource1.InsertParameters("Gender").DefaultValue = DirectCast(grid1.FooterRow.FindControl("ddGender"), DropDownList).SelectedValue
SqlDataSource1.InsertParameters("DateOfBirth").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtDOB"), TextBox).Text
SqlDataSource1.InsertParameters("ZipCode").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtZIP"), TextBox).Text
SqlDataSource1.Insert()
End If
End Sub
感谢您为解决此问题提供的任何帮助!
约翰