Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DropDownList Autopostback清除字段,恢复为默认值_C#_Asp.net_Drop Down Menu_Autopostback - Fatal编程技术网

C# DropDownList Autopostback清除字段,恢复为默认值

C# DropDownList Autopostback清除字段,恢复为默认值,c#,asp.net,drop-down-menu,autopostback,C#,Asp.net,Drop Down Menu,Autopostback,我在detailsview的两个不同字段中有两个下拉列表,第一个决定第二个字段的值 当我从第一个ddl中选择一个值时,autopostback将删除detailsview中已经存在的所有数据,并将第一个dropdownlist恢复为第一个选择 此外,当我从列表中选择(多次更改ddl选择)时,它将抛出: 诸如Eval()、XPath()和Bind()之类的数据绑定方法只能在数据绑定控件的上下文中使用 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码

我在detailsview的两个不同字段中有两个下拉列表,第一个决定第二个字段的值

当我从第一个ddl中选择一个值时,autopostback将删除detailsview中已经存在的所有数据,并将第一个dropdownlist恢复为第一个选择

此外,当我从列表中选择(多次更改ddl选择)时,它将抛出:

诸如Eval()、XPath()和Bind()之类的数据绑定方法只能在数据绑定控件的上下文中使用

描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.InvalidOperationException:诸如Eval()、XPath()和Bind()等数据绑定方法只能在数据绑定控件的上下文中使用

源错误:

[无相关来源行]

------First DDL

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SQLLEAVECODE"
DataTextField="LEAVETYPE" DataValueField="LEAVECODE"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AppendDataBoundItems="True" ViewStateMode="Enabled" AutoPostBack="True"
SelectedValue='<%# BIND("REQLEAVECODE") %>' Width="80px">
<asp:ListItem> </asp:ListItem>
</asp:DropDownList>

-------Second DDL

<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource1"
DataTextField="REASON" DataValueField="REASON" 
SelectedValue='<%# Bind("leavetype") %>'>
</asp:DropDownList>



---------page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DetailsView1.DataBind();
}
}


--------DDL 1 selectedIndexChange
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList lvtype = (DropDownList)(DetailsView1.FindControl("DropDownList1"));
DropDownList lvreason = (DropDownList)(DetailsView1.FindControl("DropDownList2"));
Label27.Text = lvtype.SelectedValue.ToString();

DetailsView1.Fields[4].Visible = true;
}
----第一个DDL
-------第二DDL
---------页面加载
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
DetailsView1.DataBind();
}
}
--------DDL 1已选择索引更改
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
DropDownList lvtype=(DropDownList)(DetailsView1.FindControl(“DropDownList1”);
DropDownList lvreason=(DropDownList)(DetailsView1.FindControl(“DropDownList2”);
Label27.Text=lvtype.SelectedValue.ToString();
DetailsView1.Fields[4].Visible=true;
}

如果
下拉列表
位于
详细视图
中,则请处理
详细视图
项目命令事件
,而不是
下拉列表1\u选择的索引已更改


希望这将帮助您

您的标记引用了
ID=“DropDownList1”
ID=“DropDownList2”


但是,事件处理程序引用了
DetailsView1.FindControl(“DropDownList6”)
DetailsView1.FindControl(“DropDownList6”)
。这可能与您的问题有关吗?

尝试一下,您可能会有一些乐趣:谢谢,我已经经历了这一过程,但我似乎遇到了问题,因为ddl位于详细信息视图中,因此我无法使级联dropdownlist在我的程序中正常工作。它越来越近了。现在,当我从DDL1中选择时,它将在回发时保留所选值,但不允许填充DDL2。这样做会导致此错误:诸如Eval()、XPath()和Bind()之类的数据绑定方法只能在数据绑定控件的上下文中使用。很抱歉,这是我的传输错误,dropdownlists被命名为5和6,但我认为这会让人困惑,因为6是第一个,5是第二个,所以为了论坛的目的,我根据顺序将它改为1和2。