I have a FormView where a row is "UserDate". My Formview can insert,edit, and delete records, but the UserDate field is supposed to be retrieved as the current date and time.
default.aspx (HTML Markup)
<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# DateTime.Now %>' />
When I run it, and Select "Insert" on the formsview, the date automatically populates. However, when I add the record, it doesn't show in the gridview while every other column value does.
All other columns are user input like this though:
<asp:TextBox ID="PO_IDTextBox" runat="server" Text='<%# Bind("PO_ID") %>' />
The gridview resembles my sql database table po_table.
How do I implement the date to the gridview / database table?
Edit:
I'm assuming I do it here in my cs file?
protected void FormView2_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
}
So far I tried:
<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# DateTime.Now %>' />
which gives the date in the formview but doesn't transfer to the gridview when adding.
I've tried:
<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# Eval(DateTime.Now) %>' />
which gives an error.
My files: (Class file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PO_1_5_15
{
public partial class _Webform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void FormView2_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
}
HTML Markup
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Test_DatabaseConnectionString %>" SelectCommand="SELECT [PO_ID], [PO_Title], [Date_Received], [Date_Completed], [Username], [PO_Note], [UserDate] FROM [PO_Table]"></asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="PO_AutoID" DataSourceID="SqlDataSource2" EnableModelValidation="True">
<EditItemTemplate>
PO_AutoID:
<asp:Label ID="PO_AutoIDLabel1" runat="server" Text='<%# Eval("PO_AutoID") %>' />
<br />
PO_ID:
<asp:TextBox ID="PO_IDTextBox" runat="server" Text='<%# Bind("PO_ID") %>' />
<br />
PO_Title:
<asp:TextBox ID="PO_TitleTextBox" runat="server" Text='<%# Bind("PO_Title") %>' />
<br />
Date_Received:
<asp:TextBox ID="Date_ReceivedTextBox" runat="server" Text='<%# Bind("Date_Received") %>' />
<br />
Date_Completed:
<asp:TextBox ID="Date_CompletedTextBox" runat="server" Text='<%# Bind("Date_Completed") %>' />
<br />
Username:
<asp:TextBox ID="UsernameTextBox" runat="server" Text='<%# Bind("Username") %>' />
<br />
UserDate:
<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# Eval(DateTime.Now) %>' />
<br />
PO_Note:
<asp:TextBox ID="PO_NoteTextBox" runat="server" Text='<%# Bind("PO_Note") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
If you want to have the same DateTime from the FormView to be displayed on the GridView, you will need to save that DateTime off somewhere. If you were to just simply use DateTime.Now in the GridView, it would always just display the current DateTime, not the time entered from the FormView.
My recommendation would be to have/add a column in your database table, as like a creation or update date. Then when you insert/update your database from the FormView, include the new DateTime in those SQL calls. Then just bind it like you are for your other columns in your GridView.
Related
I am at a total loss here! Perhaps someone will see something that I'm missing. I'm using this same code else where and it works just fine. The purpose is to seed an Edit mode FromView with data that I want to supply for the user. In this case I'm not allowing user to change the information that I'm supplying.
What I get when I try to run this is:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference
not set to an instance of an object.
My code behind checks for null reference so I'm not sure what I can do to correct this. And this works elsewhere in my code (variables different of course).
Here is a fragment from the aspx page and then the C# code behind that go with it.
<asp:Panel runat="server" ID="RejectDetailPnl" Visible="false">
<asp:Button ID="RejectBtn" runat="server" OnClick="RejectBtn_Click" BackColor="#ff0000" ForeColor="#ffffff" Text="Click To Reject" />
<asp:FormView ID="RejSubmittFV" runat="server" OnInit="RejSubmittFV_Init" DefaultMode="Edit" DataSourceID="RejSubmitDS">
<EditItemTemplate>
CAssetID:
<asp:TextBox Text='<%# Bind("CAssetID") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="CAssetIDTextBox" /><br />
Reason:
<asp:TextBox Text='<%# Bind("Reason") %>' runat="server" ID="ReasonTextBox" Width="500" Height="500" TextMode="MultiLine" OnTextChanged="ReasonTextBox_TextChanged" /><br />
Source:
<asp:TextBox Text='<%# Bind("Source") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="SourceTextBox" /><br />
RejBy:
<asp:TextBox Text='<%# Bind("RejBy") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="RejByTextBox" /><br />
RejDT:
<asp:TextBox Text='<%# Bind("RejDT") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="RejDTTextBox" /><br />
<asp:LinkButton runat="server" Text="Update" o CommandName="Update" ID="UpdateButton" CausesValidation="True" />
</EditItemTemplate>
<InsertItemTemplate>
CAssetID:
<asp:TextBox Text='<%# Bind("CAssetID") %>' runat="server" ID="CAssetIDTextBox" /><br />
Reason:
<asp:TextBox Text='<%# Bind("Reason") %>' runat="server" ID="ReasonTextBox" /><br />
Source:
<asp:TextBox Text='<%# Bind("Source") %>' runat="server" ID="SourceTextBox" /><br />
RejBy:
<asp:TextBox Text='<%# Bind("RejBy") %>' runat="server" ID="RejByTextBox" /><br />
RejDT:
<asp:TextBox Text='<%# Bind("RejDT") %>' runat="server" ID="RejDTTextBox" /><br />
<asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" /> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
</InsertItemTemplate>
<ItemTemplate>
CAssetID:
<asp:Label Text='<%# Bind("CAssetID") %>' runat="server" ID="CAssetIDLabel" /><br />
Reason:
<asp:Label Text='<%# Bind("Reason") %>' runat="server" ID="ReasonLabel" /><br />
Source:
<asp:Label Text='<%# Bind("Source") %>' runat="server" ID="SourceLabel" /><br />
RejBy:
<asp:Label Text='<%# Bind("RejBy") %>' runat="server" ID="RejByLabel" /><br />
RejDT:
<asp:Label Text='<%# Bind("RejDT") %>' runat="server" ID="RejDTLabel" /><br />
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditButton" CausesValidation="False" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource runat="server" ID="RejSubmitDS" ConnectionString="Data Source=gbaptccsr01;Initial Catalog=CInTracDB;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT CAssetID, Reason, Source, RejBy, RejDT FROM RejFailCtrl WHERE (CAssetID = #CAssetID)" UpdateCommand="UPDATE RejFailCtrl SET Reason = #Reason, Source = #Source, RejBy = #RejBy, RejDT = #RejDT WHERE (CAssetID = #CAssetID)">
<SelectParameters>
<asp:SessionParameter SessionField="ChangeRecord" Name="CAssetID"></asp:SessionParameter>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Reason"></asp:Parameter>
<asp:Parameter Name="Source"></asp:Parameter>
<asp:Parameter Name="RejBy"></asp:Parameter>
<asp:Parameter Name="RejDT"></asp:Parameter>
<asp:Parameter Name="CAssetID"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:Button runat="server" ID="RejectResetBtn" Visible="false" OnClick="RejectResetBtn_Click" Text="*** After Entering your Comment Click this bar!! ****" ForeColor="White" BackColor="Red" />
</asp:Panel>
The C# code behind and the choke point is:
TextBox uname = (TextBox)RejSubmittFV.Row.FindControl("RejByTextBox");
There are two other points I have tested by commenting out sections that also fail in this code block. Basically every time I repeat this code sequence it renders this error. Here is the full code block for your reference:
protected void RejectBtn_Click(object sender, EventArgs e)
{
TextBox uname = (TextBox)RejSubmittFV.Row.FindControl("RejByTextBox");
if (uname != null)
uname.Text = Session["RegUser"].ToString();
TextBox usource = (TextBox)RejSubmittFV.Row.FindControl("SourceTextBox");
if (usource != null)
usource.Text = "CInTrac";
TextBox udate = (TextBox)RejSubmittFV.Row.FindControl("RejDTTextBox");
if (udate != null)
udate.Text = DateTime.Now.ToString("MM/dd/yyyy");
}
Like I mentioned this procedure is working perfectly elsewhere so I don't have a clue why it isn't here.
Any ideas would be greatly appreciate!
I'm seeing two items that could be null which you are expecting to exist:
RejSubmittFV and the .Row property. They're not assigned, in the code you've provided, so we cannot be sure that you're initializing that object.
Session["RegUser"] which isn't tested for null before you're trying to call .ToString() on it.
If you're able to step through with a debugger, these are the kinds of operations to look for. You can either add a watch to these values or you can simply hover your mouse and see if each reference is null.
I never came up with a real answer for this issue! What I ended up doing to work around the problem was to eliminate the ForrView and simple sit the 5 data fields in a panel that I controlled the visibility with by ID which eliminated the issue of having to navigate the FindControl. This lead to abandoning the SqlDataSource and doing my update via a stored procedure and 100% with code behind and it was a LOT cleaner as a result.
// Update the Control file
SqlConnection UpdRejConnection = new SqlConnection("Data Source=GBAPTCCSR01;Initial Catalog=CInTracDB;Integrated Security=True");
SqlCommand Updcmd = new SqlCommand();
SqlDataReader Updreader;
Updcmd.Parameters.AddWithValue("#CAssetID", Session["ChangeRecord"]);
Updcmd.Parameters.AddWithValue("#Reason", ReasonTextBox.Text.ToString());
Updcmd.Parameters.AddWithValue("#Rejby", RejByTextBox.Text.ToString());
Updcmd.Parameters.AddWithValue("#Source", SourceTextBox.Text.ToString());
Updcmd.Parameters.AddWithValue("#RejDT", RejDTTextBox.Text.ToString());
Updcmd.CommandText = "usp_UpdRejCtrl";
Updcmd.CommandType = CommandType.StoredProcedure;
Updcmd.Connection = UpdRejConnection;
UpdRejConnection.Open();
Updreader = Updcmd.ExecuteReader();
UpdRejConnection.Close();
In the below code i have a grid view in which i have a textbox i am passing 0 and 1 to check box if it is 1 the checkbox to be checked.I tried but it is not working.Pls help me to solve the issue.
<asp:TemplateColumn HeaderText="Sales Price Ref" ItemStyle-Width="200px" HeaderStyle-VerticalAlign="Top">
<HeaderTemplate>
<asp:Label ID="lblSalesPriceRef" runat="server" Text="Sales Price"></asp:Label>
<br /><br />
<asp:TextBox runat="server" ID="txtSalesPriceRef" AutoPostBack="true" BorderStyle="Solid" BorderColor="#6495ED" BackColor="#B0C4DE" Height="20px" Width="90px" OnTextChanged="txtItem_TextChanged"></asp:TextBox>
</HeaderTemplate>
<EditItemTemplate>
<asp:CheckBox ID="ChSalesPriceRef" runat="server" Checked='<%#Eval("SalesPriceRef")=="1" ? true:false %>' />
<%-- <asp:TextBox ID="txtSalesPriceRef" Width="90px" runat="server" Text='<%#Eval("SalesPriceRef") %>'></asp:TextBox>--%>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSalesPriceRef" runat="server" Text='<%# Convert.ToString(Eval("SalesPriceRef"))== "1" ? "True" :"False" %>' > </asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
YES you can achieve it by binding it to the checkbox
as
<asp:CheckBox ID="ChSalesPriceRef" runat="server" Checked='<%# Bind("SalesPriceRef")%>' Enabled="false" />
if you allow to change checkbox value then make enable=true
I have a DetailsView that uses my Business Logic Layer in the code behind. I still use the DetailsView declaratively the same as if you drop a label control on the page but I don't declare a datasourceID in the attributes section of the XHTML.
After extensive searching on the web I noticed that most examples use a SqlDataSource declaratively and assign the id to the DetailsView datarouceID attribute.
I don't want to have the UI directly connected to the Data Logic Layer. I have a BLL that does all the busines end transactions and passes objects to the UI.
This is what my DetailsView looks like.
<asp:DetailsView ID="dvCapability"
runat="server"
Width="90%" AutoGenerateEditButton="true"
AutoGenerateRows="False"
EmptyDataText="Unable to retrieve the capability details."
BorderColor="LightGray"
BorderStyle="Solid"
BorderWidth="1px"
DataKeyNames="IAID" >
<AlternatingRowStyle CssClass="altrow" />
<EmptyDataRowStyle HorizontalAlign="Center" />
<Fields>
<asp:TemplateField HeaderText="Status:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblLabStatus" runat="server" Text='<%# Eval("LabStatus") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddLabStatus" runat="server" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("LabStatus") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblLabType" runat="server" Text='<%# Eval("LabType") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbLabType" runat="server" Text='<%# Bind("LabType") %>' Width="200px" BackColor="White" Enabled="false" Font-Size="10px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UIC:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblUIC" runat="server" Text='<%# Eval("UIC") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddUIC" runat="server" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("UIC") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Org code:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblOrgCode" runat="server" Text='<%# Eval("OrgCode") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddOrgCode" runat="server" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("OrgCode") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VSYSCOM:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblVSYSCOM" runat="server" Text='<%# Eval("VSYSCOM") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbVSYSCOM" runat="server" Text='<%# Bind("VSYSCOM") %>' Width="200px" BackColor="White" Font-Size="10px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblLabName" runat="server" Text='<%# Eval("LabName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbLabName" runat="server" Text='<%# Bind("LabName") %>' Width="500px" BackColor="White" Font-Size="10px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblLabDescription" runat="server" Text='<%# Eval("LabDescription") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbLabDescription" runat="server" Text='<%# Bind("LabDescription") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Technical Capability:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblTechnicalCapability" runat="server" Text='<%# Eval("TechnicalCapability") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbTechnicalCapability" runat="server" Text='<%# Bind("TechnicalCapability") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Facility:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblFacility" runat="server" Text='<%# Eval("Facility") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbFacility" runat="server" Text='<%# Bind("Facility") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
<ItemTemplate>
<asp:Label ID="lblComments" runat="server" Text='<%# Eval("Comments") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbComments" runat="server" Text='<%# Bind("Comments") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Fields>
<FooterStyle CssClass="footer" />
<HeaderStyle Width="15%" />
<PagerStyle CssClass="footer" />
<RowStyle CssClass="row" />
</asp:DetailsView>
I bind it to a DataTable that is returned from my BLL in the Page's codebhind like so.
Utilities.BindCompositeControl(dvCapability, Controller.GetCapability(getIaid()));
The BindCompositeControl() method is in a static class that I use to reduce redundatnt coding. The method is coded as follows:
public static void BindCompositeControl(CompositeDataBoundControl control, DataTable values)
{
control.DataSource = values;
control.DataBind();
}
However, when I try to use the Edit or Insert Buttons that are generated by the DetailsView control nothing happens. What am I missing in order to bind this control programatically and change it's operational state on the UI?
If you can post your code for your data source you are binding, that will help see if you've allowed inserting and updating which may be causing an issue.
However I suspect you may need to manually maintain the datasource as you change the mode of the DetailsView. When you click Edit / Insert it causes a postback and I believe it no longer has the datasource attached.
On the first load of the control, you'll attach the datasource but after that you need to recreate the control each time, probably by saving the control state to the ViewState, recreating the data source and pull the control state back out of the ViewState on each page load.
It sounds more complex than it is. One way to do this is by creating the datasource through the OnInit event for the page. If you need to maintain selected rows or pages, use the ViewState to maintain those details.
You should create the DataSource and bind it before the ViewState is up, so create the datasource and apply it in OnInit
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
datatype myData = /* .... */
//Create DataSource here
for (int i = 0; i < 10; i++) {
//Do something
myData.add(/* some data */);
}
dvCapability.DataSource = myData;
dvCapability.DataBind();
}
To add and delete items, you'll need to store the data in a way that is accessible to the rest of a page, such as Session[] and then remove / add items to that during the dvCapabilities_Inerting event, etc. After an add or delete you'll need to rebind the data with the updated data.
Take a gander at this http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx. This sort of explains things under the 2. Persisting static data section.
For those of you who may run into this down the line. Here's what I came up with and it works. I add a value to the ViewState dictionary and set it in the Page_OnInit event handler. In the Page_PreRender event handler is where I bind the detailsview and set it's Mode. Please note that if you are using viewstate you must bind your data and change mode after the view state has loaded. the Page's Initialize method is called before the ViewState is loaded. Hope this helps those who need it. I found many posts that weren't successfully answered regarding this topic. Much thanks to Kirk for giving me a solid answer and a great lead on where I wanted to go.
public partial class About : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
if (!IsPostBack)
{
//Initially add the view state variable
ViewState.Add("DetailsViewMode", DetailsViewMode.ReadOnly);
}
base.OnInit(e);
}
protected override void OnPreRender(EventArgs e)
{
if (ViewState["DetailsViewMode"] != null)
{
//change the mode of the DetailsView
dvTest.ChangeMode((DetailsViewMode)ViewState["DetailsViewMode"]);
//Bind the details view to a source
//This is my own custom class but this is where you yould put
//DetailsView.Datasource = <source>
//DetailsView.DataBind()
Utilities.BindCompositeControl(dvTest, NadController.GetCapability(19));
}
base.OnPreRender(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
//Must handle ModeChanging event or you'll get a run time error when binding DetailsView programatically
protected void dvTest_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
}
//Handles button clicks from the DetailsView control
protected void dvTest_OnItemCommand(object sender, DetailsViewCommandEventArgs e)
{
ViewState["DetailsViewMode"] = GetDetailsViewMode(e.CommandName);
}
protected DetailsViewMode GetDetailsViewMode(string mode)
{
switch (mode)
{
case Constants.Edit:
return DetailsViewMode.Edit;
case Constants.Insert:
return DetailsViewMode.Insert;
default:
return DetailsViewMode.ReadOnly;
}
}
}
me problem is next:
I have a DetailsView control with many editItemTemplates some work fine but i have one dont work's.
code here:
<asp:TemplateField HeaderText="Date" SortExpression="date">
<ItemTemplate>
<%#this.putDate(Eval("fecha")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="date" runat="server" ></asp:TextBox>
<asp:CalendarExtender id="CE" runat="server" Format="dd/MM/yyyy"
PopupButtonID="calImg" TargetControlID="date">
</asp:CalendarExtender>
<asp:ImageButton id="calImg" runat="server" CommandName=""
ImageUrl="img/imgCal.gif" />
</EditItemTemplate>
</asp:TemplateField>
In view mode the information appear right but in edit mode after select new date with calendar and pres accept to update information, the data dont update. Any idea?
Thz!
Replace:
<asp:TextBox ID="date" runat="server" ></asp:TextBox>
With:
<asp:TextBox ID="date" runat="server" Text='<%# Bind("SelectedDate") %>'></asp:TextBox>
I'm trying to bind the selected value from the date picker to an asp textbox but I have this Error: 'this._targetEl.value.length' is null or not an object.
Here's the code:
<InsertItemTemplate>
Book Title:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="booktitleDataSource" DataTextField="booktitle"
DataValueField="bookid" SelectedValue='<%# Bind("bookid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="booktitleDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [bookid], [booktitle] FROM [TblBooks]">
</asp:SqlDataSource>
<br />
Employee PIN:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<br />
Date borrowed:
<asp:TextBox ID="dateborrowedTextBox" runat="server" Text='<%# Bind("dateborrowed") %>' />
<%--<input type="text" name="dateborrowedTextBox" readonly="readonly" id="dateborrowedTextBox">--%>
Date Picker
<br />
<asp:Button ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:Button ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
It was working when I
tried using <input type="text" name="dateborrowed" readonly="readonly" id="dateborrowedTextBox"> but when I tried using the asp:TextBox I cant pass the selected value from date picker to the textbox. So where did I go wrong? Is there a way to program the date picker link to call the popup calendar? (its in Java btw)
Help would be much appreciated!
Thanks in advance.
Nested controls have their ID/name attributes overwritten with an absolute unique id/name when rendered. When your javascript is attempting to reference the TextBox its name won't actually be 'dateborrowedTextBox' it will be something like '...$ctl00$dateborrowedTextBox'.
If your javascript is finding the control by name, this might clear up the issue:
Date Picker
Otherwise try this if its finding the control by id:
Date Picker
Found the solution. I think it can only read asp.net syntax not classic asp.
<asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>' />
<%--Date Picker--%>
Date Picker