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.
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();
- }
- }
- }
No comments:
Post a Comment