非常常用的範例
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" ShowFooter="True" DataKeyNames="EmployeeID" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ValidationGroup="Edit" />
<asp:TemplateField HeaderText="EmployeeID" InsertVisible="False" SortExpression="EmployeeID">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("EmployeeID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="Add" OnClick="Add_Click" runat="server" ValidationGroup="Insert" >Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="editFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorFirstName" runat="server"
ValidationGroup="Edit" ControlToValidate="editFirstName" Text="*" ForeColor="Red" ErrorMessage="FirstName can not empty"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstName" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorFirstNameAdd" runat="server"
ValidationGroup="Insert" ControlToValidate="txtFirstName" Text="*" ForeColor="Red" ErrorMessage="FirstName can not empty"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="editLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorLastName" runat="server"
ValidationGroup="Edit" ControlToValidate="editLastName" Text="*" ForeColor="Red" ErrorMessage="LastName can not empty"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorLastNameAdd" runat="server"
ValidationGroup="Insert" ControlToValidate="txtFirstName" Text="*" ForeColor="Red" ErrorMessage="LastName can not empty"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="editddlCity" runat="server" SelectedValue='<%# Bind("City") %>' >
<asp:ListItem Value="" >請選擇</asp:ListItem>
<asp:ListItem>Seattle</asp:ListItem>
<asp:ListItem>Tacoma</asp:ListItem>
<asp:ListItem>Kirkland</asp:ListItem>
<asp:ListItem>Redmond</asp:ListItem>
<asp:ListItem>London</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorCity" runat="server"
ValidationGroup="Edit" ControlToValidate="editddlCity" Text="*" ForeColor="Red" ErrorMessage="City can not empty"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlCity" runat="server" >
<asp:ListItem>請選擇</asp:ListItem>
<asp:ListItem>Seattle</asp:ListItem>
<asp:ListItem>Tacoma</asp:ListItem>
<asp:ListItem>Kirkland</asp:ListItem>
<asp:ListItem>Redmond</asp:ListItem>
<asp:ListItem>London</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorCityAdd" runat="server"
ValidationGroup="Insert" ControlToValidate="txtFirstName" Text="*" ForeColor="Red" ErrorMessage="City can not empty"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataKeyNames = new string[1] { "EmployeeID" };
}
protected void Add_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["FirstName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
SqlDataSource1.InsertParameters["LastName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
SqlDataSource1.InsertParameters["City"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlCity")).SelectedValue;
SqlDataSource1.Insert();
}