我正在调试,我得到了上面列出的奇怪错误。这很奇怪,因为我的 Dropdownlist 被称为 DropDownlist1。当我将它设置为之前的值时,调试器告诉我它无法将其转换为字符串。
所以这是我的 C#,问题出在哪里。
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Gridsource.ConnectionString = "Data Source=CATALOGSERVER;Initial Catalog=UACOrders;Persist Security Info=True;User ID=Catalog;Password=pass";
Gridsource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
Gridsource.SelectCommand = "GetOrderHistory";
Gridsource.DataSourceMode = SqlDataSourceMode.DataSet;
GridView1.DataSource = Gridsource;
ControlParameter cp = new ControlParameter();
cp.ControlID = DropDownList1.ClientID;
cp.Name = "Company";
cp.PropertyName = "SelectedValue";
cp.Type = System.TypeCode.String;
Gridsource.SelectParameters.Add(cp);
Gridsource.Page = this;
this.Controls.Add(Gridsource);
GridView1.DataBind();
updatepanel1.Update();
}
这是涉及的一小部分asp.net
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="contentarea">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" ID="updatepanel1">
<ContentTemplate>
<div>
<asp:Label Text="Company: " runat="server"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="company" DataTextField="COMPANY" DataValueField="COMPANY"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="true" >
</asp:DropDownList>
<asp:SqlDataSource ID="company"
runat="server" ConnectionString="<%$ ConnectionStrings:UACOrdersConnectionString %>"
SelectCommand="SELECT [Customer] AS [COMPANY] FROM [Accounts] ORDER BY [Customer]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="Gridsource" runat="server"></asp:SqlDataSource>
奇怪的是,我可以让这种事情在 asp.net 代码中正常工作。但我似乎无法让它在这个特定网页所需的 C# 代码隐藏中工作。所以再次,我需要找出 cp.ControlID 应该真正等于什么。
具体的异常是“用户代码未处理 InvalidOperationException”。“在 ControlParameter 'Company' 中找不到控件 'DropDownList1'。”
VS 将异常放在第 39 行,即
GridView1.DataBind();
如果您需要查看更多代码,请发表评论,我会发布更多。