Asked by:
Databind problem

Question
-
User2097455209 posted
I can't seem to get a simple .NET application to recognize a databound control. Basically I have a .NET application, this time in Visual Basic, with an Entity build from an existing database. When I have tags like the following, I can't get it to do anything.
<div>
<asp:TextBox ID="tbServiceRequestDate" runat="server" Text='<%# Bind("ServiceRequestDate") %>'></asp:TextBox>
</div>Code behind:
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
tbServiceRequestDate.DataBind()
Else
tbServiceRequestDate.DataBind()
End IfEnd Sub
Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
tbServiceRequestDate.DataBind()
End SubThe Entity is basically EHSShipping.
I get this error:
System.InvalidOperationException
HResult=0x80131509
Message=Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Source=App_Web_5tsnn3n3
StackTrace:
at ASP.default_aspx.__DataBindingtbServiceRequestDate(Object sender, EventArgs e) in C:\dev\EHSShipping\EHSShipping\Default.aspx:line 14
at _Default.Page_Load(Object Sender, EventArgs e) in C:\dev\EHSShipping\EHSShipping\Default.aspx.vb:line 7What I'm trying to do is databind the controls as text boxes and select boxes (eventually, a radio button or a checkbox), and have a submit button that takes all the form data and updates the database through the entities. Once I get that working, then I'm going to build a grid control that shows all the various forms as individual records in the system that can be selected and viewed.
I figure once I get the TextBox to work right, I can do the other controls pretty easy, however, right now I'm having a hard time getting that one TextBox to update the database on submit. Keep in mind there is an ID that is unique in the database, but I was assuming the Entity Framework handles that automatically and just inserts the rows as the columns are referenced in the Bind commands...
Please advise...
Wednesday, February 17, 2021 8:18 PM
All replies
-
User475983607 posted
There's no data source and you are not using a data bound control. I recommend going through a few tutorials to learn the basics.
Also, if you are new to ASP.NET then I recommend going with MVC or better yet .Net5.0. Web Forms is a lot older than modern JavaScript based applications. You'll run into issues when you want to use fancy JavaScript libraries.
Wednesday, February 17, 2021 8:44 PM -
User1535942433 posted
Hi dl0dth,
As far as I think,you could use datasource control just like gridview or repeater.
More details,you could refer to below article:
Best regards,
Yijing Sun
Thursday, February 18, 2021 6:18 AM -
User1120430333 posted
<div> <asp:TextBox ID="tbServiceRequestDate" runat="server" Text='<%# Bind("ServiceRequestDate") %>'></asp:TextBox> </div>
I don't see how it could possibly work if you're not giving the name of the object that contains the property, like if the entity model object was named dlodth, then it would be dlodth.ServiceRequestDate. How else is it going to know where the property is at or who it belongs to? I am not a fan of bringing an EF entity up and directly addressing an EF entity in a bound user control like a textbox in a Web application. This kind of coding could get the database hacked IMHO.
Thursday, February 18, 2021 7:05 AM