How to solve the error that "Eval (), XPath (), Bind () and other data binding methods can only be used in the context of the data binding control" appears when DropDownList is implemented in FormView?

Source: Internet
Author: User
The FormView control is a control that can be integrated with display, modification, addition, and deletion. It feels very easy to use, but yesterday I found a Bug that can be said to be its, I want to achieve the linkage effect of the drop-down box. For example, if I select the drop-down box corresponding to province B in the drop-down box of province A, the city corresponding to province A will be displayed in the drop-down box of province B, what I want to achieve is the linkage between the campus and the corresponding campus buildings. This effect alone is very good, for example, the following code <asp: dropDownList ID = "DropDownList2" runat = "server" performanceid = "sqlperformance2"
DataTextField = "" DataValueField = "code" AutoPostBack = "true">
</Asp: DropDownList>
<Asp: DropDownList ID = "DropDownList3" runat = "server" performanceid = "sql0000137"
DataTextField = "floor name" DataValueField = "floor code">
</Asp: DropDownList> <asp: SqlDataSource ID = "sql0000tms" runat =" server "ConnectionString =" <% $ ConnectionStrings: SBGLConnectionString %>"
SelectCommand = "SELECT [floor name], [floor Code] FROM [C _ floor name code table] WHERE ([campus] = @ Campus)">
<SelectParameters>
<Asp: ControlParameter ControlID = "DropDownList2" Name = "campus" PropertyName = "SelectedValue"
Type = "Int32"/>
</SelectParameters>
</Asp: SqlDataSource>
<Asp: SqlDataSource ID = "SqlDataSource2" runat = "server" ConnectionString = "<% $ ConnectionStrings: SBGLConnectionString %>"
SelectCommand = "SELECT [campus Code], [campus abbreviation] FROM [C _ campus code table]"> </asp: SqlDataSource>

You only need to do this. You do not need to write code in CS.
However, an error occurs when you transfer the code to FormView. Generally, the first time you load the page, there will be no error. I don't know why, but if you change the campus, the following error will occur.
Data Binding methods such as Eval (), XPath (), and Bind () can only be used in the context of a data binding control. Note:An error occurred while executing the current Web request. Check the stack trace information for details about the error and the source of the error in the code.
Exception details:System. InvalidOperationException: data binding methods such as Eval (), XPath (), and Bind () can only be used in the context of the data binding control.
Source error:

[No relevant source line]

I still don't know which line is wrong. I checked it online and many people asked, but no one could give a correct or better answer, I haven't found a solution for a long time. It seems that I have to give it a try. It takes about half a day for me to achieve the desired effect. below is my code, for more information

Add a GridView in the aspx file and edit the inserted template to achieve the linkage effect between the campus drop-down menu and the Building Name. Pay attention to the following points:
1. To achieve linkage, set the AutoPostBack attribute in the campus drop-down box to true;
2. The data source hole corresponding to the campus drop-down box must be placed in FormView. Otherwise, an error is returned and the data source cannot be found.
3. The data source space in the drop-down box of the building corresponding to the campus should be placed outside the FormView. In fact, there is basically no relationship between the data source and the floor name drop-down box, but the data is obtained through the data source and then transmitted to DropDownList.
<Asp: FormView ID = "fv_ShiYanShi" runat = "server" DefaultMode = "Insert" Width = "437px">
<InsertItemTemplate>
<Asp: DropDownList ID = "dpl_xiaoqu" runat = "server" AutoPostBack = "True" performanceid = "sqd_xiaoqu"
DataTextField = "campus abbreviation" DataValueField = "campus code" SelectedValue = '<% # Bind ("campus") %>'
Width = "200px">
</Asp: DropDownList> <asp: SqlDataSource ID = "sqd_xiaoqu" runat = "server" ConnectionString = "<% $ ConnectionStrings: SBGLConnectionString %>"
SelectCommand = "SELECT [campus Code], [campus abbreviation] FROM [C _ campus code table]"> </asp: SqlDataSource>
<Asp: DropDownList ID = "dpl_lou" runat = "server" SelectedValue = '<% # Bind ("Building Name") %> 'width = "200px">
</Asp: DropDownList>
</InsertItemTemplate>
</Asp: FormView>
<Asp: SqlDataSource ID = "sqd_lou" runat = "server" ConnectionString = "<% $ ConnectionStrings: SBGLConnectionString %>"
SelectCommand = "SELECT [floor Code], [floor name], [campus] FROM [C _ floor name code table] WHERE ([campus] = @ Campus)" performancemode = "DataSet">
<SelectParameters>
<Asp: Parameter Name = "campus" Type = "Int32"/>
</SelectParameters>
</Asp: SqlDataSource>
<Asp: SqlDataSource ID = "SqlDataSource1" runat = "server" ConnectionString = "<% $ ConnectionStrings: SBGLConnectionString %>"
SelectCommand = "SELECT [floor Code], [floor name] FROM [C _ floor name code table] WHERE ([campus] = @ Campus)" performancemode = "DataSet">
<SelectParameters>
<Asp: Parameter Name = "campus" Type = "Int32"/>
</SelectParameters>
</Asp: SqlDataSource>
<Asp: DropDownList ID = "DropDownList1" runat = "server">
</Asp: DropDownList>
Private DropDownList dpl_xiaoqu is processed in the corresponding CS file;
Private DropDownList dpl_lou;

Protected void Page_Load (object sender, EventArgs e)
{
Dpl_xiaoqu = (DropDownList) this. fv_ShiYanShi.FindControl ("dpl_xiaoqu ");
Dpl_lou = (DropDownList) this. fv_ShiYanShi.FindControl ("dpl_lou ");
Dpl_xiaoqu_SelectedIndexChanged ();
}
Protected void dpl_xiaoqu_SelectedIndexChanged ()
{
Dpl_lou.Items.Clear ();

This. sqd_lou.SelectParameters ["campus"]. DefaultValue = this. dpl_xiaoqu.SelectedValue;

DataView datav = (DataView) this. sqd_lou.Select (DataSourceSelectArguments. Empty );
Foreach (DataRowView dr in datav)
{
ListItem li = new ListItem ();
Li. Value = dr. Row ["floor code"]. ToString ();
Li. Text = dr. Row ["floor name"]. ToString ();
Dpl_lou.Items.Add (li );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.