- Joined
- 31 Dec 2003
- Posts
- 5,172
- Location
- Barrow-In-Furness
I think that's the problem, the textbox I need to take in the UserName is inside a InsertItem template of a DetailsView.
Last edited:
' First create a new textbox variable
Dim myTextBox as New TextBox
' Assign the variable to the textbox in the DetailsView
myTextBox = CType(DetailsView1.FindControl("TextBox1"), TextBox)
' You should now have a link to the textbox via the variable you created
If myTextBox.Text = "=" Then
myTextBox.Text = Me.LoginName1.ToString()
End If
Dim myTextBox As New TextBox
myTextBox = CType(DetailsView1.FindControl("UserNameBox"), TextBox)
If myTextBox.Text = "=" Then
myTextBox.Text = Me.LoginName1.ToString()
End If
Wardie said:It's saying LoginName1 is not a member of the page
Wardie said:What does CType do, i've seen it a few times but it has never been explained.
Wardie said:You've helped me more than most of the stuff i've read dude <3
Wardie said:as i'm writing this i've probably realised that is the very problem....
myTextBox.Text = myLoginName.Page.User.Identity.Name
I don't think it will work in the page_load event, as the TextBox and LoginName
only get created when the DetailsView changes to the insert mode.
What you could do is instead of putting it under the TextChanged event, add it to the DataBinding event.
Get rid of the If statement, and to get the name you need to use the following:
Code:myTextBox.Text = myLoginName.Page.User.Identity.Name
My bad![]()
Dim myTextBox As New TextBox
myTextBox = CType(DetailsView1.FindControl("UserNameBox"), TextBox)
myTextBox.Text = "=" Then
myTextBox.Text = UserNameBox.Page.User.Identity.Name
Dim myTextBox As New TextBox
Dim myLoginName As New LoginName
myTextBox = CType(DetailsView1.FindControl("txtUsername"), TextBox)
myLoginName = CType(DetailsView1.FindControl("LoginName1"), LoginName)
myTextBox.Text = myLoginName.Page.User.Identity.Name
Dim myTextBox As New TextBox
Dim myLoginName As New LoginName
myTextBox = CType(DetailsView1.FindControl("UserNameBox"), TextBox)
myLoginName = CType(DetailsView1.FindControl("LoginName1"), LoginName)
myTextBox.Text = myLoginName.Page.User.Identity.Name
For starters, have you logged into the appliction first?
Secondly, did you put it under the DataBinding event?
Protected Sub UserNameBox_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myTextBox As New TextBox
Dim user As MembershipUser = Membership.GetUser()
myTextBox = CType(DetailsView1.FindControl("UserNameBox"), TextBox)
myTextBox.Text = user.UserName.ToString()
End Sub
<asp:TextBox ID="UserNameBox" runat="server" Text='<%# Bind("ProjectOwner") %>' [B]OnDataBinding="UserNameBox_DataBinding"[/B]></asp:TextBox>
Partial Class HoursNew
Inherits System.Web.UI.Page
Protected Sub UserNameBox_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myTextBox As New TextBox
Dim user As MembershipUser = Membership.GetUser()
myTextBox = CType(DetailsView1.FindControl("UserNameBox"), TextBox)
myTextBox.Text = user.UserName.ToString()
End Sub
End Class
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="HoursNew.aspx.vb" Inherits="HoursNew" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="ID" DataSourceID="ObjectDataSource1" DefaultMode="Insert" GridLines="None"
Height="50px" Width="303px">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:TemplateField HeaderText="WorkCode" SortExpression="WorkCode">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("WorkCode") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("WorkCode") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("WorkCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="WorkDate" SortExpression="WorkDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("WorkDate") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<form name="frmCalendar">
<input name="txtDate" type="text" />
<a href="javascript:calendar_window=window.open('Calendar.aspx?formname=frmCalendar.txtDate','calendar_window','width=350,height=250');calendar_window.focus()">
Calendar
</a>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("WorkDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hours" SortExpression="Hours">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Hours") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Hours") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Hours") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName" SortExpression="UserName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="UserNameBox" runat="server" Text='<%# Bind("UserName") %>' OnDataBinding="UserNameBox_DataBinding"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetHours"
TypeName="ManHoursTableAdapters.HoursTableAdapter" UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="Original_ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="WorkCode" Type="Int32" />
<asp:Parameter Name="WorkDate" Type="DateTime" />
<asp:Parameter Name="Hours" Type="Decimal" />
<asp:Parameter Name="Original_ID" Type="Int32" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="WorkCode" Type="Int32" />
<asp:Parameter Name="WorkDate" Type="DateTime" />
<asp:Parameter Name="Hours" Type="Decimal" />
</InsertParameters>
</asp:ObjectDataSource>
</asp:Content>