web 2.0

ASP.NET(vb.net) & FormView - Add/Insert

ASP.NET(vb.net) & FormView - Add/Insert Example scripts how to use FormView control in asp.net , Gets or sets the custom content for an item in add mode to insert a record.

ShotDev Focus:
- ASP.NET(vb.net) & FormView - Add/Insert

Example

FormViewAddInsert.aspx

<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Dim strSQL As String
Dim strGalleryName As String = ""

Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"
objConn = New OleDbConnection(strConnString)
objConn.Open()

End Sub

Sub BindData()

strSQL = "SELECT * FROM gallery WHERE GalleryName = '"& strGalleryName &"'"

Dim dtReader As OleDbDataReader
objCmd = New OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()

'*** BindData to FormView ***'
myFormView.DataSource = dtReader
myFormView.DataBind()

dtReader.Close()
dtReader = Nothing

End Sub

Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub

Sub myFormView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)

'*** Image ***'
Dim Image1 As Image = CType(myFormView.FindControl("Image1"),Image)
IF Not IsNothing(Image1) Then
Image1.ImageUrl = "images/"&myFormView.DataItem("Picture")
Image1.Width = "100"
Image1.Attributes.Add("OnClick", "window.open('images/"&myFormView.DataItem("Picture")&"')")
Image1.Style.Add("cursor","hand")
Image1.ToolTip = myFormView.DataItem("GalleryName")
End IF

'*** GalleryName ***'
Dim lblGalleryName As Label = CType(myFormView.FindControl("lblGalleryName"),Label)
IF Not IsNothing(lblGalleryName) Then
lblGalleryName.Text = myFormView.DataItem("GalleryName")
End IF

End Sub

Sub myFormView_ItemInserting(sender As Object, e As FormViewInsertEventArgs)
Dim strFileName As String
'*** GalleryName ***'
Dim txtGalleryName As TextBox = CType(myFormView.FindControl("txtGalleryName"), TextBox)

'*** If Select File Upload ***'
Dim filPicture As HtmlInputFile = CType(myFormView.FindControl("filPicture"), HtmlInputFile)

If Trim(filPicture.PostedFile.FileName) <> "" Then
strFileName = System.IO.Path.GetFileName(filPicture.Value)
filPicture.PostedFile.SaveAs(Server.MapPath("images/" & strFileName))
End If

strSQL = "INSERT INTO gallery (GalleryName,Picture) " & _
" VALUES ('" & txtGalleryName.Text & "','" & strFileName & "') "
objCmd = New OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()

strGalleryName = txtGalleryName.Text
myFormView.ChangeMode(FormViewMode.ReadOnly)
BindData()
End Sub

Sub myFormView_ModeChanging(sender As Object, e As FormViewModeEventArgs)
Select Case e.NewMode
Case FormViewMode.Edit
myFormView.ChangeMode(FormViewMode.Edit)
Case FormViewMode.ReadOnly
myFormView.ChangeMode(FormViewMode.ReadOnly)
End Select
BindData()
End Sub

</script>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
<asp:FormView id="myFormView" runat="server" DefaultMode="Insert"
OnDataBound="myFormView_DataBound"
onModeChanging="myFormView_ModeChanging"
OnItemInserting="myFormView_ItemInserting">
<ItemTemplate>
<table width="100" cellpadding="5" border="1">
<tr>
<td valign="top" align="center">
<asp:Image id="Image1" runat="server"/>
<br />
<asp:Label id="lblGalleryName" runat="server"></asp:Label>
<br />
</td>
</tr>
</table>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox id="txtGalleryName" runat="server"></asp:TextBox><br />
<input id="filPicture" type="file" runat="server"><br />
&nbsp;<asp:LinkButton id="lnkUpdate" runat="server" CommandName="Insert">Insert</asp:LinkButton>
</InsertItemTemplate>

</asp:FormView>
<asp:Label id="lblID" runat="server"></asp:Label>
</form>
</body>
</html>

Create a asp.net file and save to path root-path/dotnet/

Run
http://localhost/dotnet/FormViewAddInsert.aspx

Screenshot

ASP.NET(vb.net) & FormView - Add/Insert

ASP.NET(vb.net) & FormView - Add/Insert
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (No Ratings Yet)
Loading ... Loading ...

One Response to “ASP.NET(vb.net) & FormView - Add/Insert”

  1. 1hacking…

Leave a Reply

You must be logged in to post a comment.