ASP.net membership

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:
OK, because it is under the InsertItem template of the DetailsView, you need to find the control within the DetailsView.

Set the textbox autopostback property to 'True'.
Create a new event on the textbox using TextChanged.
To find a control within a DetailsView/GridView/FormView.. etc

Code:
' 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

Everytime you type in =, when the page refreshes it should get the login name.
 
Using the code under the TextChanged event:

Code:
  Dim myTextBox As New TextBox

        myTextBox = CType(DetailsView1.FindControl("UserNameBox"), TextBox)

        If myTextBox.Text = "=" Then
            myTextBox.Text = Me.LoginName1.ToString()
        End If

It's saying LoginName1 is not a member of the page, is it because it hasn't been declared as a variable and wont work for the same reason as the textbox?

What does CType do, i've seen it a few times but it has never been explained.

You'v helped me more than most of the stuff i've read dude <3
 
Last edited:
Wardie said:
It's saying LoginName1 is not a member of the page

Sounds like it can't find it.
Whereabouts is it on the page?

Wardie said:
What does CType do, i've seen it a few times but it has never been explained.

CType Function - Returns the result of explicitly converting an expression to a specified data type, object, structure, class, or interface.

http://msdn2.microsoft.com/en-us/library/4x2877xb(VS.80).aspx

Wardie said:
You've helped me more than most of the stuff i've read dude <3

No problem, not much happening at work atm anyway. :)
 
The LoginName label is sat next to the TextBox in the InsertItemTemplate at the moment, as i'm writing this i've probably realised that is the very problem....
 
Ok I sorted that, but when I press = is just puts "System.Web.UI.WebControls.LoginName" into the TextBox.

I really need it to do it without putting = though, so would sticking it on a page load event work?

Saying that I could just put it as "" for any of the fields that require text. I'll probably hide the field once it's properly working anyways, no need for the user to see it.
 
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 :o
 
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 :o

Cheers for that, i'll give it a go!

The DetailsView default mode is insert, if that makes any difference. On this paticular page it is being used solely for inserting data.
 
Last edited:
If I use:

Code:
Dim myTextBox As New TextBox

        myTextBox = CType(DetailsView1.FindControl("UserNameBox"), TextBox)

        myTextBox.Text = "=" Then
        myTextBox.Text = UserNameBox.Page.User.Identity.Name

It says UserNameBox is not declared, same if I use myLoginName
 
lol, no something like this...

Code:
 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

You need to find the LoginName in the DetailsView as well.
 
Not working, on the line myTextBox.Text = myLoginName.Page.User.Identity.Name I get a NullReferenceException was unhandled by user code.

ARGH LOASDLASDASG LASD<BGLASBF<ASBF<BASF (RAGE)

Code:
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?
 
For starters, have you logged into the appliction first?
Secondly, did you put it under the DataBinding event?

Tried logging in and loading the page and it still gives me that error before the page loads :(

Yes it is under the DetailsView DataBinding event.
 
Can't seem to find a way to get a UserName or ID in with the data!

I suppose I could try using a UserID but then all other SQL queries later on will get more complicated, it'd be simpler this way I suppose.

Although since I can't seem to get anything to work at the moment....

I've got two threads on the official ASP.net thread forums both unsolved :|

http://forums.asp.net/p/1163073/1934700.aspx#1934700 - Related to this thread, some suggestions from other guys but I don't really know what's going on.

I swear there really isn't much information for learning ASP.net around.

All I need to do is insert a UserID based on who they are logged in as, along with some other information, why does it have to be such a pain.
 
OK, read through the post on ASP.NET, and they have a number of solutions.
The code I posted works fine, I know because I tested it.
I have now tried it with the code posted on the ASP.NET site, which also works fine.
I also converted it to VB, as it was in C#.
You just need to put it in the right event for it to detect the textbox.
Copy this code into your vb file.
Code:
    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

Now look at the html in the aspx file, and find your textbox in the DetailsView.
It should look similar to the html below.
Don't copy the following line, just add the OnDataBinding bit at the end.
Code:
<asp:TextBox ID="UserNameBox" runat="server" Text='<%# Bind("ProjectOwner") %>' [B]OnDataBinding="UserNameBox_DataBinding"[/B]></asp:TextBox>

You can get rid of the code I posted before, and the LoginName, as you won't need this anymore.

Keep finding bits of code, try and work out what it is doing, you'll pick it up eventually.
Also run through the data tutorials exactly using the Northwind sample database, it helped me.
 
I still get the NullReferenceException unhandled by user error on the line:

myTextBox.Text = user.UserName.ToString()

HoursNew.aspx.vb


Code:
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

HoursNew.aspx

Code:
<%@ 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">
    &nbsp;<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>
                    &nbsp;<asp:TextBox ID="UserNameBox" runat="server" Text='<%# Bind("UserName") %>' OnDataBinding="UserNameBox_DataBinding"></asp:TextBox>&nbsp;
                    
                </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>

Sorry about this I know i'm probably being a pain but I can't work out what's wrong with it :(

This is basically a mock-up version of the program and when i've got it working i'll re-create it with much better code (comments and everything named properly etc)

Thanks for helping dude.
 
Back
Top Bottom