views:

376

answers:

3

When users go to a specified page, I have a DetailsView attached to a SqlDataSource and already setup in Insert mode. I'm essentially using it as a registration page for certain events. Rather than having the users type in their "UserName", I want that field to automatically populate based on that user already being logged in.

Does anyone have any suggestions on how to either have the User.Identity.Name be the default value that appears on Page_load, or how to code an override on DetailsViewInsertEventArgs? Of course, if I'm totally off-base, then other suggestions would be great.

I'm using c# code behind.

Thanks, Mike

+1  A: 

You can do this:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DetailsView1.DefaultMode = DetailsViewMode.Insert;
            if (DetailsView1.FindControl("TextBox1") != null)
            {
                TextBox txt1 = (TextBox)DetailsView1.FindControl("TextBox1");
                txt1.Text = User.Identity.Name.ToString();
            }
        }
    }
Ricardo
A: 

Ah, Thanks Ricardo. That makes sense, but I'm still having trouble getting it to work. I guess I forgot to mention that the control is in a boundfield, if that makes a difference.

Here's the mainpage code:

<div align="center">
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        DataKeyNames="RegistrationID" DataSourceID="SqlDataSourceFBReg" 
        DefaultMode="Insert" Height="50px" 
        Width="55%" OnItemInserted="NFLElim" >
        <Fields>
            <asp:BoundField DataField="RegistrationID" HeaderText="RegistrationID" 
                InsertVisible="False" ReadOnly="True" SortExpression="RegistrationID" />
            <asp:BoundField DataField="UserName" HeaderText="One Season User Name" 
                SortExpression="UserName" />
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />...

And here's how I have the code behind setup:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NFLElim_Reg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DetailsView1.DefaultMode = DetailsViewMode.Insert;
            if (DetailsView1.FindControl("UserName") != null)
            {
                TextBox txt1 = (TextBox)DetailsView1.FindControl("UserName");
                txt1.Text = User.Identity.Name.ToString();
            }
        }

    }
    protected void NFLElim(object sender, DetailsViewInsertedEventArgs e)
    {
        Response.Redirect("Account.aspx");
    }

}

Hopefully I inserted the code correctly

MGumbel
That should do it, you inserted the code correctly. This should work even in a bound field.
Ricardo
A: 

TextBox tata = (TextBox)DetailsView1.Rows[6].Cells[1].Controls[0]; tata.Text=User.Identity.Name;

This will chane boundfield. If you change your boundfield to templatefield, this code can also work:

TextBox txt1 = (TextBox)DetailsView1.FindControl("UserName"); txt1.Text = User.Identity.Name.ToString();

egiray