Hi kelsen1989,
Refer below sample.
HTML
<asp:FormView ID="CustomerFormView" DataSourceID="CustomerDetailsSqlDataSource" DataKeyNames="CustomerId"
GridLines="Both" runat="server">
<HeaderStyle BackColor="Navy" ForeColor="White" />
<RowStyle BackColor="White" />
<EditRowStyle BackColor="LightCyan" />
<ItemTemplate>
CustomerId:
<asp:Label ID="CustomerIdLabel" runat="server" Text='<%# Eval("CustomerId") %>' />
<br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>' />
<br />
Country:
<asp:Label ID="CountryLabel" runat="server" Text='<%# Bind("Country") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="New" />
</ItemTemplate>
<EditItemTemplate>
CustomerId:
<asp:Label ID="CustomerIdLabel1" runat="server" Text='<%# Eval("CustomerId") %>' />
<br />
Name:
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' TextMode="MultiLine" />
<br />
Country:
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>' TextMode="MultiLine" />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Name:
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' TextMode="MultiLine" />
<br />
Country:
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>' TextMode="MultiLine" />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="CustomerDetailsSqlDataSource" SelectCommand="SELECT [CustomerId], [Name], [Country] FROM [Customers]"
InsertCommand="INSERT INTO [Customers] ([Name], [Country]) VALUES (@Name, @Country)"
UpdateCommand="UPDATE [Customers] SET [Name] = @Name, [Country] = @Country WHERE [CustomerId] = @CustomerId"
DeleteCommand="DELETE FROM [Customers] WHERE [CustomerId] = @CustomerId" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
OnInserted="CustomerDetailsSqlDataSource_OnInserted" runat="server" OnUpdating="CustomerDetailsSqlDataSource_OnUpdating">
<DeleteParameters>
<asp:Parameter Name="CustomerId" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="CustomerId" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Code
C#
protected void CustomerDetailsSqlDataSource_OnInserted(object sender, SqlDataSourceStatusEventArgs e)
{
string name = (CustomerFormView.FindControl("txtName") as TextBox).Text.Replace(Environment.NewLine, "<br />");
string country = (CustomerFormView.FindControl("txtCountry") as TextBox).Text.Replace(Environment.NewLine, "<br />");
e.Command.Parameters["@Name"].Value = name;
e.Command.Parameters["@Country"].Value = country;
}
protected void CustomerDetailsSqlDataSource_OnUpdating(object sender, SqlDataSourceCommandEventArgs e)
{
string name = (CustomerFormView.FindControl("txtName") as TextBox).Text.Replace(Environment.NewLine, "<br />");
string country = (CustomerFormView.FindControl("txtCountry") as TextBox).Text.Replace(Environment.NewLine, "<br />");
e.Command.Parameters["@Name"].Value = name;
e.Command.Parameters["@Country"].Value = country;
}
VB.Net
Protected Sub CustomerDetailsSqlDataSource_OnInserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
Dim name As String = (TryCast(CustomerFormView.FindControl("txtName"), TextBox)).Text.Replace(Environment.NewLine, "<br />")
Dim country As String = (TryCast(CustomerFormView.FindControl("txtCountry"), TextBox)).Text.Replace(Environment.NewLine, "<br />")
e.Command.Parameters("@Name").Value = name
e.Command.Parameters("@Country").Value = country
End Sub
Protected Sub CustomerDetailsSqlDataSource_OnUpdating(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs)
Dim name As String = (TryCast(CustomerFormView.FindControl("txtName"), TextBox)).Text.Replace(Environment.NewLine, "<br />")
Dim country As String = (TryCast(CustomerFormView.FindControl("txtCountry"), TextBox)).Text.Replace(Environment.NewLine, "<br />")
e.Command.Parameters("@Name").Value = name
e.Command.Parameters("@Country").Value = country
End Sub
Screenshot
