Wednesday, May 27, 2009

Fill asp.net formview textbox to initial value in insert mode

In most scenarios, the asp.net data controls like detailsview and formview have empty controls in insert mode. This means any field that requires [or at least expects] user input will not contain any data. But this default configuration sometimes needs to be modified. Say, we would like to fill the date field in a textbox to current date, so that it will be easier for user. This is just an example. This can be the asp.net dropdownlist from where we would like to select one value initially, or it may be other asp.net user input control like checkbox or radiobutton. In such scenario, we can set the control value to some default initial value. With datacontrols like formview and detailsview, it takes a little effort [simple and tricky!]. In this short post [In fact I am expecting to keep this post as such as possible since there is no need of much explanation: concept is crystal clear and implementation is also easy!] I am going to show the simple coding practice to initiate the formview or detailsview control to some value. I will be choosing formview to show the example.


Fig: Setting a textbox to an initial value in insert mode of an asp.net formview control

The formview can have one of three modes at one instance of time. They are Insert, Edit and ReadOnly modes. We should be careful about the current mode of the formview before we do reference any control. This is because a textbox in edit template may not be available in the read only mode, for instance. So we will check the current mode of the formview and search for the textbox with its ID. Then we can easily set the value of the control. Let's have a formview first.

FormsView Design Markup

<asp:FormView ID="FormView1" runat="server" HeaderText="Fill the Credentials below." DefaultMode="Insert">

<ItemTemplate>

<table>

<tr>

<td>

Name: <asp:Label ID="lblName" runat="server"></asp:Label>

</td>

</tr>

<tr>

<td>

Date of Test: <asp:Label ID="lblTestDate" runat="server"></asp:Label>

</td>

</tr>

</table>

</ItemTemplate>

<InsertItemTemplate>

<table>

<tr>

<td>

Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td>

Date of Test: <asp:TextBox ID="txtTestDate" runat="server"></asp:TextBox>

</td>

</tr>

</table>

</InsertItemTemplate>

</asp:FormView>

We simply have two fields in the formview: name and date of test. Our intension will be to initiate the value of the date of test field to current date.

CodeBehind for the example

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Call to filling textbox in the insert mode;
//I assume here that initially the formview
//is in insert mode
FillDefaultVaueInFormView();
}
}

public void FillDefaultVaueInFormView()
{
//if the formview is in insert mode
if (FormView1.CurrentMode == FormViewMode.Insert)
{
//txtTestDate is the id of the 'date of text' textbox in formview
TextBox txtTest = FormView1.FindControl("txtTestDate") as TextBox;
if (txtTest != null)
{
txtTest.Text = DateTime.Now.ToShortDateString();
}
}
}

Now the formview will have the date of test field initiated to the current date. Let me repeat- Simple and Tricky! You can post your comments and suggestions from the form below. You can rate this post as well. You can even share this post. Just add this post to the social sites! Happy Programming!

kick it on DotNetKicks.com Shout it pimp it

9 comments:

oldeast said...

Thanks worked well for me..

Anonymous said...

What if you don't want to reference a specific control, but want to iterate through all controls in the EditTemplate?
I can iterate through FormView1.Controls[0].Controls[1].Controls[0].Controls, but is there a more direct way to get to the EditTemplate controls?

Power said...

This worked very well for me. I'm now looking for a way to customize the way that the date is displayed. In my case, I'm using the date as part of an ID number for records in a database:
YYYY-MM-DD-#####

How can I get the date to show up like this, so that the user just has to enter in the ##### at the end of the string?

Anonymous said...

thanks, I had tried filling up the form for two days

Bernie Greene said...

Thank you. I've been looking for exactly how to set a textbox value in a formview for the last couple of hours.

Anonymous said...

Thank you very much! I am very happy because it worked pretty cool for me. I have been struggling with this for a long while. Really appreciate it and keep up the good work!

Please, if I can asked for more. I am trying to also Insert an expiry date where I will have to Add one (1) year to the initial date so as to get the expiry date from the initial date.

If anyone can help, i will appreciate it.

Cheers!

Sangam Uprety said...

Dear Anonymous,
You can add 1 year in the current date time as: DateTime.Now.AddYears(1);
Similarly, to add 1 years to your mydate variable:
mydate.Now.AddYears(1);

Thanks.

samso said...

I Cannot thank you enough, everything worked well! But I have a problem with the FormView. functionality.

Here is the Scenarios:

1. When the Form loads (Page_Load()) at First, the default date works and inserts into the database as expected. But as soon I want to Insert another set of data into the database i.e Second time, the default date disappears until I reload the page afresh. And usually, the users are expected to have the form load (open) and they can continue to perform Inserts several times as needed.

2. Please, how can I also make the User ID(Current logged on User) as part of the default data so that we can track and know who insert what data.

I will seriously appreciate it if you can help me out on these two scenarios and thanking you in advance.

Warm regards,

samso said...

I Cannot thank you enough, everything worked well! But I have a problem with the FormView. functionality.

Here is the two Scenarios:

1. When the Form loads (Page_Load()) at First, the default date works and inserts into the database as expected. But as soon I want to Insert another set of data into the database i.e Second time, the default date disappears until I reload the page afresh. And usually, the users are expected to have the form load (open) and they can continuously perform Inserts several time.

2. How can I also make the User ID(Current logged on User) as part of the default data so that we can track and know who insert the what data.

I will seriously appreciate if you can help me out on these two scenarios and thanking you in advance.

Warm regards,

Post a Comment

Hope you liked this post. You can leave your message or you can put your valuable suggestions on this post here. Thanks for the sharing and cooperation!

Popular Posts

Recent Articles