Link to home
Start Free TrialLog in
Avatar of ferguson_jerald
ferguson_jerald

asked on

How to hide an asp:textfield and exclude it from the form layout, or pass a value without using the asp:textfield?

Hello Experts,

I have an asp.net application with a form that captures some information that is stored in a MS SQL database.  I would like to know who creates each record.  I currently have an asp:textbox that captures the current user with:  
creator_txt.Text = System.Web.HttpContext.Current.User.Identity.Name;

Open in new window

Then I use that text field in a SqlCommand and insert it into a MS SQL Server table using:
SqlCommand new_record_cmd = new SqlCommand("Insert Into tbl(record_nm, creator) Values @record_nm, @creator)))", lcms_cnxn);
                new_record_cmd.Parameters.AddWithValue("@record_nm", record_nm_txt.Text);
                new_record_cmd.Parameters.AddWithValue("@creator", creator_txt.Text);

                lcms_cnxn.Open();
                new_record_cmd.ExecuteNonQuery();
                lcms_cnxn.Close();

Open in new window

It all works just fine, but I would like this field to be hidden - it doesn't need to be visible in the form.  
Do I need this text field at all?  
If yes, how do I hide the field from the layout and still pass the value of creator_txt to the database?
If not, how do I pass the current user to @creator without using the text field?

I hope this makes sense.  Please ask questions if it's not clear.

I am using Visual Studio 2012, SQL Server Management Studio 2012, asp.net and c#.

Thanks,
J
SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi you can use Hiddenfield or Label to do this.
Avatar of ferguson_jerald
ferguson_jerald

ASKER

Thanks.  But what do I do to my code behind to get it to work properly?  I currently have:

creator_txt.Text = System.Web.HttpContext.Current.User.Identity.Name;

Open in new window

.
What do I replace the
.Text
in
creator_txt.Text
with?  When I remove it I get
Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.HiddenField
.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks - it worked as expected.