I am working on a page written on VB 2010 for viewing or searching information and also editing or adding new information. I have both forms in the same, ItemTemplate and EditItemTemplate. I have added a button at the bottom of the page to enable the edit
function.
Can anyone help me how come up with a solution where edit button is available on the itemtemplate and save button on the edititemtemplate form? And, what sort of code needs to go behind the scenes?
Then in your code behind, you have to have something like this:
Protected Sub Button_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
If e.CommandName = "Save" Then
'Do whatever it is supposed to do in this case
ElseIf e.CommandName = "Update" Then
'Do whatever it is supposed to do in this case
End If
End Sub
Note that, for both buttons (Save and Edit) the OnCommand routine is the same. I hope this helps :)
Yes, that's happening because you are giving the query but you are not providing it with the parameters the query needs (@Email, @FullName, @Login, etc). You will need to declare them. For each parameter you will need to add the following:
Sorry, but there is no question in your last post. Your code looks fine to me. Are you finding any problems with it? Just be aware that, for your OnTextChanged event to fire, you need to add AutoPostBack="True".
You mean searchbox2? Well, what might be happening is that your textbox is inside another control (such as a griview, for example). If this is the case, whenever you need to use ii, you need to use FindControl. So, let's suppose your search box is inside
a gridview. you would have to do something like this:
For Each row as GridViewRows in yourGridView
Dim searchBox as TextBox = row.FindControl("searchbox2")
'...
'more code doing something with your search box
End For
If this doesn't solve your problem, please give more details about the problem and post your code here.
lions1855
Member
8 Points
61 Posts
Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 25, 2012 06:46 PM|LINK
I am working on a page written on VB 2010 for viewing or searching information and also editing or adding new information. I have both forms in the same, ItemTemplate and EditItemTemplate. I have added a button at the bottom of the page to enable the edit function.
Can anyone help me how come up with a solution where edit button is available on the itemtemplate and save button on the edititemtemplate form? And, what sort of code needs to go behind the scenes?
Thanks
Ana D.
Member
139 Points
86 Posts
Re: Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 25, 2012 09:06 PM|LINK
You can add the buttons in the ItemTemplate and the EditItemTemplate normally:
<ItemTemplate> ... <asp:Button ID="Button1" runat="server" Text="Edit" OnCommand="Button_Command" CommandName="Update"/> ... </ItemTemplate> ... <EditItemTemplate> ... <asp:Button ID="Button2" runat="server" Text="Save" OnCommand="Button_Command" CommandName="Save"/> ... </EditItemTemplate>Then in your code behind, you have to have something like this:
Protected Sub Button_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) If e.CommandName = "Save" Then 'Do whatever it is supposed to do in this case ElseIf e.CommandName = "Update" Then 'Do whatever it is supposed to do in this case End If End SubNote that, for both buttons (Save and Edit) the OnCommand routine is the same. I hope this helps :)
lions1855
Member
8 Points
61 Posts
Re: Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 25, 2012 11:21 PM|LINK
The button seems to be work with the exception that now when I click on the "save" button, the website returns saying that
Must declare the scalar variable "@Email".
Protected Sub Button_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
If e.CommandName = "Save" Then
SqlDataSource1.SelectCommand = "INSERT [Users] SET [Email] = @Email, [FullName] = @FullName, [Login] = @Login, [Extension] = @Extension, [Address] = @Address, [Department] = @Department, [State] = @State, [FirstName] = @FirstName, [LastName] = @LastName, [MiddleName] = @MiddleName, [HireDate] = @HireDate, [OfficePhone] = @OfficePhone, [ReportsTo] = @ReportsTo, [ZipCode] = @ZipCode, [HomePhone] = @HomePhone, [Status] = @Status, [Details] = @Details, [Office] = @Office, [City] = @City WHERE [ID] = @original_ID"
SqlDataSource1.DataBind()
ElseIf e.CommandName = "Update" Then
SqlDataSource1.SelectCommand = "UPDATE [Users] SET [Email] = @Email, [FullName] = @FullName, [Login] = @Login, [Extension] = @Extension, [Address] = @Address, [Department] = @Department, [State] = @State, [FirstName] = @FirstName, [LastName] = @LastName, [MiddleName] = @MiddleName, [HireDate] = @HireDate, [OfficePhone] = @OfficePhone, [ReportsTo] = @ReportsTo, [ZipCode] = @ZipCode, [HomePhone] = @HomePhone, [Status] = @Status, [Details] = @Details, [Office] = @Office, [City] = @City WHERE [ID] = @original_ID"
SqlDataSource1.DataBind()
End If
End Sub
Ana D.
Member
139 Points
86 Posts
Re: Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 28, 2012 03:32 PM|LINK
Yes, that's happening because you are giving the query but you are not providing it with the parameters the query needs (@Email, @FullName, @Login, etc). You will need to declare them. For each parameter you will need to add the following:
(Refer to this forum for more info: http://stackoverflow.com/questions/485821/how-to-pass-variable-to-selectcommand-of-sqldatasource)
Does it work now?
lions1855
Member
8 Points
61 Posts
Re: Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 31, 2012 03:23 PM|LINK
Ok, I got it to work and it is doing just fine. However, I would like to add a search box. My front end code read like this:
<td class="style208"><asp:TextBox ID="searchbox2" runat="server" style="text-align: left" Width="109px" CssClass="style6" ontextchanged="searchbox2_TextChanged"></asp:TextBox> </td> <td class="style181" align="center"><asp:Button ID="Button4" runat="server" Height="19px" Text="Search" Width="75px" CssClass="style6" onclick="Button4_Click" />
and ...the vb.net code read like:
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs)
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("AccessAccountsConnectionString").ToString())
' Create a DataSet object.
Dim SqlDataSource1 As New DataSet()
' Create a SELECT query.
Dim strSelectCmd As String = "SELECT Users.*, Assets.* FROM Assets INNER JOIN Users ON Assets.[ID] = Users.[ID] WHERE Login Like '% & @Login & %'"
Dim da As New SqlDataAdapter(strSelectCmd, conn)
conn.Open()
' Fill the DataTable named "USERS" in DataSet with the rows
' returned by the query.
' Bind the FormView control.
da.Fill(SqlDataSource1, "Users")
FormView1.DataSource = SqlDataSource1
FormView1.DataBind()
End Using
End Sub
Ana D.
Member
139 Points
86 Posts
Re: Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 31, 2012 03:40 PM|LINK
Sorry, but there is no question in your last post. Your code looks fine to me. Are you finding any problems with it? Just be aware that, for your OnTextChanged event to fire, you need to add AutoPostBack="True".
lions1855
Member
8 Points
61 Posts
Re: Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 31, 2012 05:06 PM|LINK
Yes, for got to add the question. The error is search_box is not declared. It may be inaccessible due to its protection level.
Ana D.
Member
139 Points
86 Posts
Re: Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 31, 2012 05:15 PM|LINK
You mean searchbox2? Well, what might be happening is that your textbox is inside another control (such as a griview, for example). If this is the case, whenever you need to use ii, you need to use FindControl. So, let's suppose your search box is inside a gridview. you would have to do something like this:
For Each row as GridViewRows in yourGridView Dim searchBox as TextBox = row.FindControl("searchbox2") '... 'more code doing something with your search box End ForIf this doesn't solve your problem, please give more details about the problem and post your code here.
lions1855
Member
8 Points
61 Posts
Re: Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 31, 2012 05:19 PM|LINK
The search box is inside of formview1. The idea is to enter some text that will query the sql table a populate the information back to formview1.
Ana D.
Member
139 Points
86 Posts
Re: Language VB ASPX Visual Studio 2010 - ItemTemplate and EditItemTemplate
May 31, 2012 05:23 PM|LINK
So the idea is the same: to access the search box control, you will have to use FindControl. Take a look in this link: http://stackoverflow.com/questions/4476168/asp-net-vb-how-to-access-controls-inside-a-formview-from-the-code-behind