I am trying to assign a FormView value to a VB variable.
I am using code that works fine when first_nameTextBox is a TextBox and user enters data directly, but fails when first_nameTextBox is a label populated from a database via FormView.
Error I receive is BC30451: 'FormView2_first_nameTextBox' is not declared. It may be inaccessible due to its protection level.
Any help much appreciated.
The code is below;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%# Page Language="VB" %>
<%# Import Namespace="System.Net.Mail" %>
<%# Import Namespace="System.Text" %>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" dir="ltr">
<head runat="server">
<!-- Scripting area here -->
<script runat="server" type="text/vbscript">
Protected Sub btnSubmit_click (ByVal sender As Object, ByVal e As EventArgs)
If IsPostBack Then
Dim sc As SmtpClient = New SmtpClient("relay.hostinguk.net")
Dim sb As StringBuilder = New StringBuilder()
Dim msg As MailMessage = Nothing
sb.Append("Name : " + FormView2_first_nameTextBox.Text + vbCrLf)
Try
msg = New MailMessage("from#company.com", _
"to#company.com", "Contact details for Co", _
sb.ToString())
sc.Send(msg)
Catch ex As Exception
' something bad happened
Response.Write("Something bad happened! - please try again")
Finally
Multiview1.SetActiveView(ViewConfirmation)
If Not msg Is Nothing Then msg.Dispose()
End Try
End If
End Sub
Protected Sub Page_Load (ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Try
Multiview1.SetActiveView(ViewForm)
Catch ex As Exception
' something bad happened
Response.Write("Something bad happened! - please try again")
End Try
End If
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:MultiView runat="server" id="MultiView1">
<asp:View runat="server" id="ViewForm">
<asp:AccessDataSource ID="AccessDataSource2bd" runat="server" DataFile="../app_data/bw_data.mdb" SelectCommand="SELECT * FROM student WHERE student_ID = 92">
</asp:AccessDataSource>
<asp:FormView runat="server" id="FormView2" DataSourceID="AccessDataSource2bd" DataKeyNames="student_ID" DefaultMode="ReadOnly">
<ItemTemplate >
<asp:Label id="first_nameTextBox" runat="server" Text='<%# Eval("first_name") %>' />,
</ItemTemplate >
</asp:FormView>
<asp:Table runat="server" id="Table2">
<asp:TableRow runat="server">
<asp:TableCell runat="server" HorizontalAlign="Left" height="20px">
<asp:Button id="UpdateButton" runat="server" CommandName="Update" onclick="btnSubmit_click" Text="Send Email"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:View>
<asp:View runat="server" id="ViewConfirmation">
<p>An email has been sent, and copied to you, confirming all current contact details.</p>
</asp:View>
<p></p>
</asp:MultiView>
</form>
</body>
</html>
Thanks
Ant
Try this:
dim tb as TextBox = FormView2.Row.FindControl("first_nameTextBox")
sb.Append("Name : " + tb.Text + vbCrLf)
Related
I have a page in a VB NET 2005 web project. On page load, it takes data from the logged in user, looks up a set of documents owned by that user, and creates a "download" button for each. Currently, this button opens a new window that streams the document, but we'd like the button itself to trigger a download.
I've got some options for this, but they all seem kind of hacky, and while I haven't found any good solutions, this seems like it should be a solved problem.
The buttons are in a form (so postback with hidden fields is one of the "hacky" options).
Let's say I have a GridView with 3 columns: Filename, Size and a Download button. Then set the CommandName and CommandArgument of the button. For security reason, it might be a better idea to use the user's document id instead of the full path name like I did. You can get the real file using combination of current user and the document id.
<asp:TemplateField HeaderText="Filename">
<ItemTemplate>
<asp:Label ID="lblFilename" runat="server" Text='<%# Bind("Filename") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Size">
<ItemTemplate>
<asp:Label ID="lblSize" runat="server" Text='<%# Bind("Size") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:Button ID="btnDownload" runat="server" Text="Download"
CommandName="Download" CommandArgument='<%# Eval("FullName") %>' />
</ItemTemplate>
</asp:TemplateField>
This is the class which I use to hold the list of files for download. A list of this class binds to the GridView.
Public Class MyFileInfo
Public Property Filename As String
Public Property Fullname As String
Public Property Size As Long
End Class
Last is handling the Download button click. I use the CommandName and CommandArgument to pass what button is click and what file it needs to give to the browser. I use stream and write to the browser with proper MIME type in the header.
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Download" Then
DownloadFile(e.CommandArgument)
End If
End Sub
Sub DownloadFile(filename As String)
Using fs = New FileStream(filename, FileMode.Open)
Response.BufferOutput = False
Response.AddHeader("Content-Length", fs.Length.ToString())
Response.AppendHeader("Content-Disposition", "attachment;filename=" & Path.GetFileName(filename))
Response.ContentType = "application/msword"
fs.Position = 0
fs.CopyTo(Response.OutputStream)
Response.Flush()
Response.End()
End Using
End Sub
Full code (aspx):
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Filename">
<ItemTemplate>
<asp:Label ID="lblFilename" runat="server" Text='<%# Bind("Filename") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Size">
<ItemTemplate>
<asp:Label ID="lblSize" runat="server" Text='<%# Bind("Size") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:Button ID="btnDownload" runat="server" Text="Download"
CommandName="Download" CommandArgument='<%# Eval("FullName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Full code (code behind):
Imports System.IO
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then BindGridList()
End Sub
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Download" Then
DownloadFile(e.CommandArgument)
End If
End Sub
Sub BindGridList()
GridView1.DataSource = GetFiles()
GridView1.DataBind()
End Sub
Function GetFiles() As List(Of MyFileInfo)
Dim listFileInfos = New List(Of MyFileInfo)
Dim dirInfo = New DirectoryInfo(Server.MapPath("~/Data"))
Dim fileInfos = dirInfo.GetFiles("*.doc")
For Each fileInfo In fileInfos
listFileInfos.Add(New MyFileInfo With {.Filename = fileInfo.Name, .Size = fileInfo.Length, .Fullname = fileInfo.FullName})
Next
Return listFileInfos
End Function
Sub DownloadFile(filename As String)
Using fs = New FileStream(filename, FileMode.Open)
Response.BufferOutput = False
Response.AddHeader("Content-Length", fs.Length.ToString())
Response.AppendHeader("Content-Disposition", "attachment;filename=" & Path.GetFileName(filename))
Response.ContentType = "application/msword"
fs.Position = 0
fs.CopyTo(Response.OutputStream)
Response.Flush()
Response.End()
End Using
End Sub
End Class
Public Class MyFileInfo
Public Property Filename As String
Public Property Fullname As String
Public Property Size As Long
End Class
I have a tab container within an update panel. I'd like to add generated tabs to that container on a button click. The tabs get added. Once, the next time the button's clicked the previous tab is lost and a new one replaces it. I want to keep the tabs if possible. These tabs will later be filled with controls based on which button is clicked.
I've found many posts suggesting that the tabs have to be re-created on PostBack but it's not working. I don't think I know how to do it either.
My current aspx file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" ViewStateMode="Enabled">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="Add Tab" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server"
ViewStateMode="Enabled">
</ajaxToolkit:TabContainer>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Here is the code-behind so far.
Imports AjaxControlToolkit
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub WebForm1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim newTab As TabPanel = New TabPanel
Dim rand As New Random
Dim exampleID As String = rand.Next(1000).ToString
newTab.ID = exampleID
newTab.HeaderText = exampleID
newTab.Controls.Add(New LiteralControl(exampleID))
TabContainer1.Tabs.Add(newTab)
Label1.Text = Label1.Text + "<br />" + "Added tab " + newTab.ID.ToString 'Just here for output.
End Sub
End Class
I'm sure the current open tabs have to be stored somewhere and re-created on the update panel's postback but like I said. Where do I put that code and how is it stored?
Any help would be much appreciated! Thanks!
PROBLEM
When i put
<asp:FileUpload ID="FileUpload1" runat="server" class="multi" />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload All" /> outside the update panel everything works fine,but as soon as i put it back into the update panel...it stops working.
ASPX CODE
<%# Page Language="VB" AutoEventWireup="false" CodeFile="try.aspx.vb" Inherits="AdminPanel_try" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="../js/multi-upload/jquery-1.3.2.js" type="text/javascript"></script>
<script src="../js/multi-upload/jquery.MultiFile.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" class="multi" />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload All" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpload" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
CODE BEHIND
Partial Class AdminPanel_try
Inherits System.Web.UI.Page
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Try
' Get the HttpFileCollection
Dim hfc As HttpFileCollection = Request.Files
For i As Integer = 0 To hfc.Count - 1
Dim hpf As HttpPostedFile = hfc(i)
If hpf.ContentLength > 0 Then
hpf.SaveAs(Server.MapPath("MyFiles") & "\" & System.IO.Path.GetFileName(hpf.FileName))
Response.Write("<b>File: </b>" & hpf.FileName & " <b>Size:</b> " & hpf.ContentLength & " <b>Type:</b> " & hpf.ContentType & " Uploaded Successfully <br/>")
End If
Next i
Catch ex As Exception
End Try
End Sub
End Class
NOTE:
i am using this tutorial kindly check this link
The UpdatePanel does not support all ASP.NET controls, FileUpload being one of them (http://msdn.microsoft.com/en-us/library/bb386454.aspx).
There are several examples on the Internet, and even here on StackOverflow, that will show you how to support asynchronous file uploads in ASP.NET.
I have the following webform with textboxes that are pre-populated on page load:
<%# Page Title="" Language="VB" MasterPageFile="~/default.master" AutoEventWireup="true" CodeFile="admin.aspx.vb" Inherits="admin" Theme="G2M" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<form id="form1" Runat="Server">
<label>Username: </label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<label>Password: </label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<label>Product Type: </label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<label>SMTP Default Only: </label>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<label>Logo: </label>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<br />
<asp:Button ID="submit" Text="Submit changes" runat="server" OnClick="SubmitChanges" />
</form>
</asp:Content>
And the codebehind is as follows:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim xmlDoc As New XmlDocument
xmlDoc.Load(Server.MapPath("~/XML_Config/Config.xml"))
Dim configValues As XMLParser = New XMLParser(xmlDoc) ''Instantiate the XMLParser
''Populate textboxes with XML data
TextBox1.Text = configValues.UserName
TextBox2.Text = configValues.Password
TextBox3.Text = configValues.ProductType
TextBox4.Text = configValues.SMTPDefaultOnly
TextBox5.Text = configValues.Logo
End Sub
Public Sub SubmitChanges(ByVal sender As Object, ByVal e As System.EventArgs)
Dim xmlDoc As New XmlDocument
xmlDoc.Load(Server.MapPath("~/XML_Config/Config.xml"))
Dim configValues As XMLParser = New XMLParser(xmlDoc) ''Instantiate the XMLParser
configValues.SMTPDefaultOnly = TextBox4.Text
End Sub
All I'm trying to do is make the values editable so when the form is presented to the user, they can change the values and submit them back to the file. My problem is that when the SubmitChanges function is called, even though I change the value of the textbox, it is still the same. How do I pass a new value, typed into thetextbox, to the function?
Enclose your setter in If Not ispostback in that page load. It's overwriting the boxes.
I'm trying to create a simple example of an editable gridview, and for some reason can't seem to get the basics working. Why is this example not displaying the label Bar and a textbox when I click on "edit"?
aspx:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="gv.aspx.vb" Inherits="WebRoot.gv" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gv" runat="server" AutoGenerateEditButton="true" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Foo">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Foo" />
<asp:Label ID="lblQuarter" runat="server" Text='<%# Eval("fooVal") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblQuarter" runat="server" Text='Bar' />
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("fooVal") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
code behind:
Public Class MyFoo
Public ReadOnly Property FooVal() As String
Get
Return _val
End Get
End Property
Private _val As String = String.Empty
Public Sub New(ByVal val As String)
_val = val
End Sub
End Class
Partial Public Class gv
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim l As New List(Of MyFoo)
l.Add(New MyFoo("first"))
l.Add(New MyFoo("second"))
gv.DataSource = l
gv.DataBind()
End Sub
Private Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
Dim x As String
x = "foo"
End Sub
End Class
Try hooking up OnRowEditing to your gv_RowEditing method. I'm surprised you don't get an error page, something like "fired event RowEditing which wasn't handled".
<asp:GridView ID="gv" OnRowEditing="gv_RowEditing" ...
Update
My bad. I assumed c# and keep forgetting to check the language. Put this in your rowediting method and the edit will work. But there's more to do in the cancel and update events.
gv.EditIndex = e.NewEditIndex
Dim l As New List(Of MyFoo)
l.Add(New MyFoo("first"))
l.Add(New MyFoo("second"))
gv.DataSource = l
gv.DataBind()
More details here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting.aspx