Vous êtes sur la page 1sur 10

Fill asp.

net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

dotNETspidor: A dot net programming blog


Experiences of an C#, asp.net web application developer with SQL, jquery, xml
Home Get Hired

Labels
ADO.NET (1) Ajax (2) ASP.NET (46) ASP.NET Control (9) ASP.NET Deployment (1) ASP.NET Design (3) asp.net DetailsView (2) ASP.NET GridView (9) asp.net membership provider (1) Business Logic Layer (1) C# (4) CSS (2) Data Access Layer (1) database (1) DataTable (3) DropdownList (2) Email (2) Error Handling (5) Files (1) JavaScript (12)
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.

1 di 10

06/01/2012 5.06

Fill asp.net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

jquery (4) ListBox (2) Nepal (1) ObjectDataSource (2) open source (1) Oracle Database (12) Oracle Error (6) Oracle Interview Questions (1) Oracle PL/SQL Programming (2) performance (1) Positive Change (1) Social Work (1) SQL Server 2005 (5) TOAD (1) User Control (1) video (1) Visual Studio (2) Visual Studio 2010 (1) xml (1) XMLHTTP (1) Home About Contact ASP.NET Oracle Twitter
01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.

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
view plain print ?

<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="lblTestD

2 di 10

06/01/2012 5.06

Fill asp.net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

ate" runat="server"></asp:Label> 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. </td> </tr> </table> </ItemTemplate> <InsertItemTemplate> <table> <tr> <td> Name: <asp:TextBox ID="txtName" runat ="server"></asp:TextBox> </td> </tr> <tr>

Find us on Facebook

Followers

DotNetSpidor
Like 29 people like DotNetSpidor.

with Google Friend Connect

Members (16)

Bijaya

Safa

Facebook social plugin

Already a member? Sign in

Love your inbox?


Enter your email address:

<td> Date of Test: <asp:TextBox ID="txtTes tDate" runat="server"></asp:TextBox> </td>

Delivered by FeedBurner
</tr> </table> </InsertItemTemplate> </asp:FormView>

Follow me!

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

3 di 10

06/01/2012 5.06

Fill asp.net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

view plain

print

01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.

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

Blog Archive

About Me
SA NGA M UP RE TY V IE W M Y C OM PL ET E PR O F IL E

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! pimp it

By .net programmer Sangam Uprety at 5:42 PM

4 di 10

06/01/2012 5.06

Fill asp.net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

Recommend this on Google

Labels: ASP.NET, ASP.NET Control Reactions:

3 comments:

oldeast said...
Thanks worked well for me..
Sun Jun 21, 12:33:00 PM

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?
Tue Jul 07, 09:35:00 PM

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-#####

5 di 10

06/01/2012 5.06

Fill asp.net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

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?
Mon Jul 27, 09:24:00 PM

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!

Comment as:

Links to this post Create a Link

6 di 10

06/01/2012 5.06

Fill asp.net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

Laurea On Line www.uniecampus.it 5 facolt senza test di ammissione con e-Campus. Contattaci ora! Jobs in Italy www.Experteer.com Thousands of quality jobs over 80K in Europe. Find yours today! UML Java, C++, C#, SQL .. www.modeliosoft.com open source modeler & extensions. expert code generation & reverse Call Java from C# www.ezjcom.com Call Java classes from C# or VB. Fast automatic 2-way bridge builder

Newer Post

Home

Older Post

Popular Posts
How to Create DataTable Programmatically in C#, ASP.NET ? Most of the times programmers fill the DataTable from database. This action fills the schema of the database table into the DataTable . We ... Open New Window in ASP.NET web page using JavaScript I have found much tricks in different tutorials and forums on opening new window in asp.net web page, using JavaScript, jquery etc. Here I h... System.Data.OracleClient requires Oracle client software version 8.1.7 or

7 di 10

06/01/2012 5.06

Fill asp.net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

greater I have oracle database installed in my computer. My Operating system is XP. I wrote an application in asp.net. It connected to oracle databa... Click the button on keypress in asp.net TextBox In this post, I am explaining the button click functionality on key press in a textbox. I am using javascript to link the input textbox and ... List all files in a folder in an asp.net gridview We do have a folder (and a number of sub folders) with a number of various files in those folders (and sub folders). Now we do need to list ... Asp.net Eval() and Bind() expressions in data bound controls like GridView and DetailsView The asp.net Eval() and Bind() expressions are heavily used in asp.net GridView and DetailsView data bound controls. The difference between t... How to Display ASP.NET GridView Header and Footer When No Data is Present? The default behaviour of asp.net gridview is that it hides both Gridview header and footer when no data is present. You can note the absence... Refresh parent page from child window using javascript in asp.net web application Much often we open child windows from parent web page . In the child page we perform some activities, and later close the window. At the ver... Move Items from one ListBox to another ListBox in asp.net web application Movement of items from one ListBox to another ListBox in asp.net is a typical issue sometimes programmers face. However in windows applicati... Bind asp.net dropdownlist to xml file Fig: Binding asp.net dropdownlist control to an xml file Today I am discussing the simple way to bind an asp.net dropdownlist control to an...

8 di 10

06/01/2012 5.06

Fill asp.net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

Recent Articles
Prove that programmers too can be social change makers : last day to vote for a social work CamStudio - Free Streaming Video Desktop Recording Software Working with dropdownlist or combobox using jquery Missing File menu in visual studio 2010 Disable button in asp net web page to prevent multiple clicks

Recent Comments
Vinod commented on open new window in aspnet web page_28: it works good for me ,But window is Not fix .it is resize Anonymous commented on last time i got following error http: Many Thanks. It helped a lot Net Framework 4 commented on last time i got following error http: Thanks i m doing this since last 2 hours . but i found Anonymous commented on bind aspnet dropdownlist to xml file: For those it's not working, you need to add "using kakada eng commented on list all files in folder in aspnet: thank you so much
Powered by Blogger Tutorials

9 di 10

06/01/2012 5.06

Fill asp.net formview textbox to initial value in insert mode | dotNETspidor: A dot net programming blog

http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html

Twitter Updates
really enthralled by the dream.. and hence request all to vote http://t.co/9wqTNqol
6 days ago

I love this dream for a better world! Vote for it here http://t.co/nvFshu2A #betterworld #sunsuperdreams 6 days ago Disable button in asp net web page to prevent multiple clicks http://t.co /WgEMBZF4 97 days ago http://t.co/VUjfIWsd 98 days ago http://t.co/3QJND6nK 98 days ago http://t.co/KCjvbiYd 98 days ago http://t.co/gXWqX1YL 98 days ago follow me on Twitter

Copyright (c) 2011 dotNETspidor: A dot net programming blog. Designed by Wordpress Templates

10 di 10

06/01/2012 5.06

Vous aimerez peut-être aussi