命令字段中的更新和删除按钮工作正常。插入不起作用....没有错误。有任何想法吗?谢谢!!
重申一下,更新和删除按钮会更新数据库。
以下是三个部分,代码,代码隐藏和sqldatasource。
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="ID" DataSourceID="SqlDataSource2" ShowFooter="True">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbInsert" runat="server" OnClick="lbInsert_Click" CausesValidation="False">INSERT</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ServerName" SortExpression="ServerName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ServerName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ServerName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ServiceName" SortExpression="ServiceName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ServiceName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ServiceName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Version" SortExpression="Version">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Version") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Version") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:DBADASHBOARDDBConnectionString %>" DeleteCommand="DELETE
FROM [DBADASHTABLE] WHERE [ID] = @ID" InsertCommand="INSERT INTO
[DBADASHTABLE] ([ServerName], [ServiceName], [Version]) VALUES (@ServerName,
@ServiceName, @Version)" SelectCommand="SELECT * FROM [DBADASHTABLE]"
UpdateCommand="UPDATE [DBADASHTABLE] SET [ServerName] = @ServerName,
[ServiceName] = @ServiceName, [Version] = @Version WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="ServerName" Type="String" />
<asp:Parameter Name="ServiceName" Type="String" />
<asp:Parameter Name="Version" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ServerName" Type="String" />
<asp:Parameter Name="ServiceName" Type="String" />
<asp:Parameter Name="Version" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
{
public class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void lbInsertClick(object sender, EventArgs e)
{
SqlDataSource2.insertparameters["ServerName"].DefaultValue =
((TextBox)GridView2.footerrow.FindControl(TextBox4)).Text;
SqlDataSource2.insertparameters["ServiceName"].DefaultValue =
((TextBox)GridView2.footerrow.FindControl(TextBox5)).Text;
SqlDataSource2.insertparameters["Version"].DefaultValue =
((TextBox)GridView2.footerrow.FindControl(TextBox6)).Text;
SqlDataSource2.Insert();
}
}
}
}