G
Guest
Hi,
Tried using the FindControl() but no luck in finidng this damn textbox
having id txtFirstName. Can someone help me with this method?
This is what I have been doing but it doesn't work,
Dim txtFName As TextBox
txtFName = CType(myDataGrid.FindControl("txtFirstName"), TextBox)
If Not (txtFName Is Nothing) Then
Label2.Text = txtFName.Text
Else
Label2.Text = "Control Not Found"
End If
Thank you for your help.
Joe
Here is my code:
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!-- #include file=dsn.aspx -->
<script language="VB" runat="server" >
Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
' Save the referrer Url
ViewState("ReferrerUrl") = Request.UrlReferrer.ToString()
BindData()
End If
End Sub
Sub BindData()
dim strsql as string
'check for an id
If Request.QueryString.Item("id") Is Nothing Then
response.redirect("Resultslist.aspx")
Else
strsql="select * from [Results] where [ID]=" &
request.querystring("id")
End If
'Create a connection string
Dim connString as String
connString = getdsn()
'Open a connection
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()
'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
'Get a datareader
Dim objDataReader as OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConnection)
'Do the DataBinding
myDataGrid.DataSource = objDataReader
myDataGrid.DataBind()
'Close the datareader/db connection
objDataReader.Close()
End Sub
Sub saveRec(Sender As Object, e As System.EventArgs)
dim strSQL as string
'check for an id
If Request.QueryString.Item("id") Is Nothing Then
response.redirect("Resultslist.aspx")
Else
strSQL="Update Results set first_name=@fn, last_name=@ln,
city=@ct where ID=" & request.querystring("id")
End If
'Create a connection string
Dim connString as String
connString = getdsn()
'Open a connection
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()
'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
objCommand.Parameters.Add("@fn", OleDbType.VarChar, 255)
objCommand.Parameters.Add("@ln", OleDbType.VarChar, 255)
objCommand.Parameters.Add("@ct", OleDbType.VarChar, 255)
objCommand.Parameters("@fn").Value = txtFirstName.Text
objCommand.Parameters("@ln").Value = txtLastName.Text
objCommand.Parameters("@ct").Value = txtCity.Text
Try
Dim txtFName As TextBox
txtFName = CType(myDataGrid.FindControl("txtFirstName"), TextBox)
If Not (txtFName Is Nothing) Then
Label2.Text = txtFName.Text Else
Label2.Text = txtFName.Text '"Control Not Found"
End If
Catch Ex as Exception
Response.Write("<p><strong>An Error Occurred:</strong> " & Ex.ToString()
& "</p>" & vbCrLf)
Finally
objConnection.Close()
End Try
End Sub
Sub cancel(Sender As Object, e As EventArgs)
Response.Redirect(ViewState("ReferrerUrl").ToString())
End Sub
</script>
<body>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="myDataGrid"
runat="server"
AutoGenerateColumns="False"
width="80%">
<ItemStyle Font-Size="X-Small"></ItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Bold="True" ForeColor="White"
BackColor=""></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<b>Results detail</b>
</HeaderTemplate>
<ItemTemplate>
<table border="0" Cellpadding="4" Cellspacing="0" Width="80%"
style="FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, sans-serif">
<tr><td valign="top" unselectable="on">first_name</td>
<td><asp:TextBox id="txtFirstName" runat="server" Width="109px" Text='<%#
Container.DataItem("first_name")%>' /></td>
</tr>
<tr><td valign="top">last_name</td><td><asp:TextBox id="txtLastName"
runat="server" Width="109px" Text='<%# Container.DataItem("last_name")%>'
/></td></tr>
<tr><td valign="top">city</td><td><asp:TextBox id="txtCity" runat="server"
Width="109px" Text='<%# Container.DataItem("city")%>' /></td></tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<br />
<asp:Button ID="updateBtn" Runat="server" Text="Save Changes"
onclick="saveRec" />
<asp:Button ID="cancelBtn" Runat="server" Text="Cancel"
onclick="cancel" />
<asp:Label TEXT="" ID="Label2" Runat="Server" />
</form>
</center>
</body>
</html>
Tried using the FindControl() but no luck in finidng this damn textbox
having id txtFirstName. Can someone help me with this method?
This is what I have been doing but it doesn't work,
Dim txtFName As TextBox
txtFName = CType(myDataGrid.FindControl("txtFirstName"), TextBox)
If Not (txtFName Is Nothing) Then
Label2.Text = txtFName.Text
Else
Label2.Text = "Control Not Found"
End If
Thank you for your help.
Joe
Here is my code:
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!-- #include file=dsn.aspx -->
<script language="VB" runat="server" >
Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
' Save the referrer Url
ViewState("ReferrerUrl") = Request.UrlReferrer.ToString()
BindData()
End If
End Sub
Sub BindData()
dim strsql as string
'check for an id
If Request.QueryString.Item("id") Is Nothing Then
response.redirect("Resultslist.aspx")
Else
strsql="select * from [Results] where [ID]=" &
request.querystring("id")
End If
'Create a connection string
Dim connString as String
connString = getdsn()
'Open a connection
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()
'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
'Get a datareader
Dim objDataReader as OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConnection)
'Do the DataBinding
myDataGrid.DataSource = objDataReader
myDataGrid.DataBind()
'Close the datareader/db connection
objDataReader.Close()
End Sub
Sub saveRec(Sender As Object, e As System.EventArgs)
dim strSQL as string
'check for an id
If Request.QueryString.Item("id") Is Nothing Then
response.redirect("Resultslist.aspx")
Else
strSQL="Update Results set first_name=@fn, last_name=@ln,
city=@ct where ID=" & request.querystring("id")
End If
'Create a connection string
Dim connString as String
connString = getdsn()
'Open a connection
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()
'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
objCommand.Parameters.Add("@fn", OleDbType.VarChar, 255)
objCommand.Parameters.Add("@ln", OleDbType.VarChar, 255)
objCommand.Parameters.Add("@ct", OleDbType.VarChar, 255)
objCommand.Parameters("@fn").Value = txtFirstName.Text
objCommand.Parameters("@ln").Value = txtLastName.Text
objCommand.Parameters("@ct").Value = txtCity.Text
Try
Dim txtFName As TextBox
txtFName = CType(myDataGrid.FindControl("txtFirstName"), TextBox)
If Not (txtFName Is Nothing) Then
Label2.Text = txtFName.Text Else
Label2.Text = txtFName.Text '"Control Not Found"
End If
Catch Ex as Exception
Response.Write("<p><strong>An Error Occurred:</strong> " & Ex.ToString()
& "</p>" & vbCrLf)
Finally
objConnection.Close()
End Try
End Sub
Sub cancel(Sender As Object, e As EventArgs)
Response.Redirect(ViewState("ReferrerUrl").ToString())
End Sub
</script>
<body>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="myDataGrid"
runat="server"
AutoGenerateColumns="False"
width="80%">
<ItemStyle Font-Size="X-Small"></ItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Bold="True" ForeColor="White"
BackColor=""></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<b>Results detail</b>
</HeaderTemplate>
<ItemTemplate>
<table border="0" Cellpadding="4" Cellspacing="0" Width="80%"
style="FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, sans-serif">
<tr><td valign="top" unselectable="on">first_name</td>
<td><asp:TextBox id="txtFirstName" runat="server" Width="109px" Text='<%#
Container.DataItem("first_name")%>' /></td>
</tr>
<tr><td valign="top">last_name</td><td><asp:TextBox id="txtLastName"
runat="server" Width="109px" Text='<%# Container.DataItem("last_name")%>'
/></td></tr>
<tr><td valign="top">city</td><td><asp:TextBox id="txtCity" runat="server"
Width="109px" Text='<%# Container.DataItem("city")%>' /></td></tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<br />
<asp:Button ID="updateBtn" Runat="server" Text="Save Changes"
onclick="saveRec" />
<asp:Button ID="cancelBtn" Runat="server" Text="Cancel"
onclick="cancel" />
<asp:Label TEXT="" ID="Label2" Runat="Server" />
</form>
</center>
</body>
</html>