Default Values ​​for Insert DetailsView

When users go to the specified page, I have a DetailsView connected to the SqlDataSource and already configured in Insert mode. I use it as a registration page for certain events. Instead of having the user type in my "UserName", I want this field to be automatically populated based on the fact that the user is already logged in.

Does anyone have any suggestions on how User.Identity.Name will be the default value that appears on the __load page or how to encode an override in DetailsViewInsertEventArgs? Of course, if I'm completely out of the database, then the other offers will be great.

I am using C # code.

Thanks Mike

+4
source share
4 answers

You can do it:

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(); } } } 
+3
source

Oh, thanks, Ricardo. It makes sense, but it's still hard for me to get it to work. I guess I forgot to mention that the control is in the border field, if that matters.

Here is the main page 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 is how I have the code behind the 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"); } } 

Hope I inserted the code correctly

0
source

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

This will be the chane boundfield field. If you change your template field border field, this code may also work:

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

0
source

Not sure if that's what you want to do, but if you want to use Session, this is a very simple solution. I actually just studied it myself.

If you save your username in a session, you can pull it directly from there as a parameter into your SQLDataSource.

All I did was add the SessionParameter to the InsertParameter list in the markup view.

In your case, I would recommend going to the markup for the SqlDataSourceFBReg control and finding the InsertParameters list.

Then add asp: SessionParameter with Name = "UserName" and SessionField = "regardless of session name"

0
source

All Articles