I am trying to dynamically bind to a repeater. The code below shows both my codebehind and my ASPX page.
To give you some background as to why I am doing it this way is because when I try to statically place the Datasource on the page. I cannot pass the variable that I need to the datasource
The query being this "SELECT resume_file_size, resume_file_type, resume_name, date_uploaded, resume_file_name, viewedcount FROM resumes WHERE (uid = @uid)",
the @uid is actually the GUID that is provided to the user. when I try to pass that to a static Datasource as through a querystring or session variable it treats it as a string and prevents it from being used as a comparison to a GUID. so I thought of doing
it the way I have it down below.
Over all if someone can tell me how to do it my initial way please do. But if not can someone explain how to fix the way I have it below?
Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
SqlConnection cnn =
new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\jobs.mdf;Integrated
Security=True;User Instance=True");
SqlDataAdapter da =
new SqlDataAdapter("SELECT resume_file_size, resume_file_type, resume_name, date_uploaded, resume_file_name,
viewedcount FROM resumes WHERE (uid = @uid)", cnn);
I wouldn't suggest doing it this way, but for the sake of brevity for you, the solution to this problem is to change your code from <% Eval("date_uploaded"); %> to <%# Eval("date_uploaded") %>. The <% %> syntax is effectively a wrapper for Response.Write, so
it doesn't allow the use of Eval, XPath, or Bind. The <%# %> syntax is only called when the parent control is being data bound, therefore it allows Eval.
Marked as answer by rexlin on Nov 29, 2006 01:30 AM
Your suggestion worked for that specific issue Thank you for that.
but after running into that I came up with a much larger issue.
With the repeater I do not have delete,Update options... This is the reason I initially wanted to go with either a gridview or Details view but the problem I had with that is when I create the sql query to pull the info from the database I need to use the
Uniqueidentifier field for a comparison. The problem I am having with that is the SQLDataSource does not allow for the UniqueIdentifier to be used. It keeps telling me something to the extent of.. "Cannot cast UniqueIdentifier as String"
Do you know how I can use a Gridview or Detailsview with using a uniqueIdentifier as a comparison in the SQL Query?
When I wanted to relate two dropdown list , I got same errors ....Then I tried using using usercontrol for the related dropdownlist and added this user control to details view and binded the public properties of user control in html . Things work fine....
You can get detailed writeup in http://aspdotnetprojects.googlepages.com/home
I have run into the same error with two dropdownlists in a fromview. After going through your solution on the URL you reference, I couldn't determine where to add the code in my application to get and set the properties you describe, step 5, does this go
in a new class, the .ascx file codebehind, where?
In my code, I added the set and get property code to the code behind page of the control, Is this correct?
Now the control shows up in my form view edititem template but I'm not clear on how you bind to the control. In my case. The control has two dropdownlists as you described in the example. If I click on the smart tag and click edit databindings how do I bind
to BOTH the city and country? Can you add more than one field in a binding as your example seems to state?
<div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">Bhat:</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times,
serif">Thanks for your reply and assistance. I think I'm doing everything you have described but I must not be binding the user control correctly. Correct me if I'm wrong but the user control can only be bound to one field? The user control created has two
fields, city and country?</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">Is there a way to bind both? My normal binding for
each would be <%# bind("country") %> <%# bind("city") %></div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">In my webform, I'm
using two fields, Contractor and JobTitle. The various contractors can have several to no child job titles. The same type of parent, child relationship as country and city.</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div>
<div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">Here is the code I'm using...</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times
new roman, new york, times, serif">This is the user control.... DisplayJobTitle.ascx </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">
This is the user control code behind page.... DisplayJobTitle ascx.cs </div>
<div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DisplayJobTitle : System.Web.UI.UserControl
{
public string contractor
{
get
{
return ddl_Contractors.SelectedValue.ToString();
}
set
{
ddl_Contractors.SelectedValue = value;
}
}
public string jobTitle
{
get
{
return ddl_JobTitles.SelectedValue.ToString();
}
set
{
ddl_JobTitles.SelectedValue = value;
}
}
}
This is formview within an edititemtemplate on my webform that is trying to do the cascadding dropdown and be bound....Staff_Edit.aspx</div>
<div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div>
<div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">
I get this Error:
InvalidCastException was unhandled by user code. Specified cast is not valid</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times
new roman, new york, times, serif">ERROR is from this line.. <uc1:DisplayJobTitle ID="DisplayJobTitle1" runat="server" Visible='<%# Bind("contractorID") %>' />.. the usercontrol in the webform</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman,
new york, times, serif"> </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">I would assume it's because the binding isn't working...?</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div>
When you say "HTML" view do you mean the Source view in Visual Studio?
I tried binding the two dropdownlists in the user control. ( see code below) but still get the... Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control, ERROR.
Is this how you were suggesting to bind the fields?
scripter26
Member
441 Points
191 Posts
Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of...
Nov 25, 2006 08:19 PM|LINK
I am trying to dynamically bind to a repeater. The code below shows both my codebehind and my ASPX page.
To give you some background as to why I am doing it this way is because when I try to statically place the Datasource on the page. I cannot pass the variable that I need to the datasource
The query being this "SELECT resume_file_size, resume_file_type, resume_name, date_uploaded, resume_file_name, viewedcount FROM resumes WHERE (uid = @uid)",
the @uid is actually the GUID that is provided to the user. when I try to pass that to a static Datasource as through a querystring or session variable it treats it as a string and prevents it from being used as a comparison to a GUID. so I thought of doing it the way I have it down below.
Over all if someone can tell me how to do it my initial way please do. But if not can someone explain how to fix the way I have it below?
Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
protected void Page_Load(object sender, EventArgs e)
{
}
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table cols=3> <tr> <td colspan=3> </td> </tr>
</HeaderTemplate> <ItemTemplate> <tr> <td align=left> "resume_file_name"); %> </td> <td align=right> "date_uploaded"); %> </td>Coasties.com
NikoKardia
Member
111 Points
24 Posts
Re: Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the contex...
Nov 26, 2006 01:02 AM|LINK
scripter26
Member
441 Points
191 Posts
Re: Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the contex...
Nov 28, 2006 07:50 PM|LINK
Your suggestion worked for that specific issue Thank you for that.
but after running into that I came up with a much larger issue.
With the repeater I do not have delete,Update options... This is the reason I initially wanted to go with either a gridview or Details view but the problem I had with that is when I create the sql query to pull the info from the database I need to use the Uniqueidentifier field for a comparison. The problem I am having with that is the SQLDataSource does not allow for the UniqueIdentifier to be used. It keeps telling me something to the extent of.. "Cannot cast UniqueIdentifier as String"
Do you know how I can use a Gridview or Detailsview with using a uniqueIdentifier as a comparison in the SQL Query?
I hope that makes sense.....
Coasties.com
scripter26
Member
441 Points
191 Posts
Re: Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the contex...
Nov 28, 2006 08:09 PM|LINK
Disregard that last message I figured out how to allow a gridview
SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\jobs.mdf;Integrated Security=True;User Instance=True"); SqlDataAdapter SQLda = new SqlDataAdapter("SELECT date_uploaded, resume_name, viewedcount FROM resumes WHERE (uid = @uid)", cn);SQLda.SelectCommand.Parameters.Add(
"@uid", SqlDbType.UniqueIdentifier).Value = userId; DataSet SQLds = new DataSet();SQLda.Fill(SQLds,
"resumes");GridView1.DataSource = SQLds.Tables[
"resumes"];GridView1.DataBind();
Coasties.com
bhatn
Member
222 Points
61 Posts
Re: Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the contex...
Dec 10, 2006 01:14 PM|LINK
When I wanted to relate two dropdown list , I got same errors ....Then I tried using using usercontrol for the related dropdownlist and added this user control to details view and binded the public properties of user control in html . Things work fine....
You can get detailed writeup in http://aspdotnetprojects.googlepages.com/home
Feedback appreciated
Bhat Np
MCAD
bhatnp@gmail.com/bhatnp@yahoo.com
JimAmigo
Member
581 Points
267 Posts
Re: Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the contex...
Dec 19, 2006 09:23 PM|LINK
Bhat:
I have run into the same error with two dropdownlists in a fromview. After going through your solution on the URL you reference, I couldn't determine where to add the code in my application to get and set the properties you describe, step 5, does this go in a new class, the .ascx file codebehind, where?
Add two public properties for country and city
(For ex Public Property country() As String
Get
Return CountryDropDownList.SelectedValue
End Get
Set(ByVal value As String)
countryDropDownList.SelectedValue = value
End Set
End Property)
Thank you for your help!
JimAmigo
Member
581 Points
267 Posts
Re: Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the contex...
Dec 19, 2006 10:14 PM|LINK
Bhat:
In my code, I added the set and get property code to the code behind page of the control, Is this correct?
Now the control shows up in my form view edititem template but I'm not clear on how you bind to the control. In my case. The control has two dropdownlists as you described in the example. If I click on the smart tag and click edit databindings how do I bind to BOTH the city and country? Can you add more than one field in a binding as your example seems to state?
Thank you for your help.
bhatn
Member
222 Points
61 Posts
Re: Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the contex...
Dec 27, 2006 10:24 AM|LINK
JimAmigo
Member
581 Points
267 Posts
Re: Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the contex...
Dec 28, 2006 02:21 PM|LINK
<%@ Control Language="C#" ClassName="JobTitles" AutoEventWireup="true" CodeFile="DisplayJobTitle.ascx.cs" Inherits="DisplayJobTitle" %> <asp:DropDownList ID="ddl_Contractors" runat="server" DataSourceID="ods_Contractors" DataTextField="ShortDescription" DataValueField="ContractorID" AutoPostBack="True"> </asp:DropDownList> / <asp:DropDownList ID="ddl_JobTitles" runat="server" DataSourceID="ods_JobTitles" DataTextField="JobTitle" DataValueField="JobTitleID"> </asp:DropDownList><asp:ObjectDataSource ID="ods_JobTitles" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetJobTitlesByContractorID" TypeName="BMSDataSetTableAdapters.JobTitlesTableAdapter"> <SelectParameters> <asp:ControlParameter ControlID="ddl_Contractors" Name="contractorID" PropertyName="SelectedValue" Type="Decimal" /> </SelectParameters> </asp:ObjectDataSource> <asp:ObjectDataSource ID="ods_Contractors" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetContractors" TypeName="BMSDataSetTableAdapters.ContractorsTableAdapter"> </asp:ObjectDataSource>This is the user control code behind page.... DisplayJobTitle ascx.cs </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">
This is formview within an edititemtemplate on my webform that is trying to do the cascadding dropdown and be bound....Staff_Edit.aspx</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> I get this Error: InvalidCastException was unhandled by user code. Specified cast is not valid</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">ERROR is from this line.. <uc1:DisplayJobTitle ID="DisplayJobTitle1" runat="server" Visible='<%# Bind("contractorID") %>' />.. the usercontrol in the webform</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">I would assume it's because the binding isn't working...?</div> <div style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </div>
JimAmigo
Member
581 Points
267 Posts
Re: Error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the contex...
Dec 28, 2006 03:22 PM|LINK
When you say "HTML" view do you mean the Source view in Visual Studio?
I tried binding the two dropdownlists in the user control. ( see code below) but still get the... Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control, ERROR.
Is this how you were suggesting to bind the fields?
<%@ Control Language="C#" ClassName="JobTitles" AutoEventWireup="true" CodeFile="DisplayJobTitle.ascx.cs" Inherits="DisplayJobTitle" %> <asp:DropDownList ID="ddl_Contractors" runat="server" DataSourceID="ods_Contractors" DataTextField="ShortDescription" DataValueField="ContractorID" SelectedValue='<%# Bind("ContractorID") %>' AutoPostBack="True"> </asp:DropDownList> / <asp:DropDownList ID="ddl_JobTitles" runat="server" DataSourceID="ods_JobTitles" DataTextField="JobTitle" DataValueField="JobTitleID" SelectedValue='<%# Bind("JobTitleID") %>'> </asp:DropDownList><asp:ObjectDataSource ID="ods_JobTitles" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetJobTitlesByContractorID" TypeName="BMSDataSetTableAdapters.JobTitlesTableAdapter"> <SelectParameters> <asp:ControlParameter ControlID="ddl_Contractors" Name="contractorID" PropertyName="SelectedValue" Type="Decimal" /> </SelectParameters> </asp:ObjectDataSource> <asp:ObjectDataSource ID="ods_Contractors" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetContractors" TypeName="BMSDataSetTableAdapters.ContractorsTableAdapter"> </asp:ObjectDataSource>