Friday 24 May 2013

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


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
  1. <asp:FormView ID="FormView1" runat="server" HeaderText="Fill the Credentials below." DefaultMode="Insert">  
  2.   
  3.             <ItemTemplate>  
  4.   
  5.                 <table>  
  6.   
  7.                     <tr>  
  8.   
  9.                         <td>  
  10.   
  11.                             Name: <asp:Label ID="lblName" runat="server"></asp:Label>  
  12.   
  13.                         </td>  
  14.   
  15.                         </tr>  
  16.   
  17.                     <tr>  
  18.   
  19.                         <td>  
  20.   
  21.                             Date of Test: <asp:Label ID="lblTestDate" runat="server"></asp:Label>  
  22.   
  23.                         </td>  
  24.   
  25.                     </tr>  
  26.   
  27.                 </table>  
  28.   
  29.             </ItemTemplate>  
  30.   
  31.             <InsertItemTemplate>  
  32.   
  33.                 <table>  
  34.   
  35.                     <tr>  
  36.   
  37.                         <td>  
  38.   
  39.                             Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>  
  40.   
  41.                         </td>  
  42.   
  43.                         </tr>  
  44.   
  45.                     <tr>  
  46.   
  47.                         <td>  
  48.   
  49.                             Date of Test: <asp:TextBox ID="txtTestDate" runat="server"></asp:TextBox>  
  50.   
  51.                         </td>  
  52.   
  53.                     </tr>  
  54.   
  55.                 </table>  
  56.   
  57.             </InsertItemTemplate>  
  58.   
  59.         </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
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if (!IsPostBack)  
  4.         {  
  5.             //Call to filling textbox in the insert mode;  
  6.             //I assume here that initially the formview  
  7.             //is in insert mode  
  8.             FillDefaultVaueInFormView();  
  9.         }  
  10.     }  
  11.   
  12.     public void FillDefaultVaueInFormView()  
  13.     {  
  14.         //if the formview is in insert mode  
  15.         if (FormView1.CurrentMode == FormViewMode.Insert)  
  16.         {  
  17.             //txtTestDate is the id of the 'date of text' textbox in formview  
  18.             TextBox txtTest = FormView1.FindControl("txtTestDate"as TextBox;  
  19.             if (txtTest != null)  
  20.             {  
  21.                 txtTest.Text = DateTime.Now.ToShortDateString();  
  22.             }  
  23.         }  
  24.     }  

No comments:

Post a Comment