【问题标题】:ASP.NET Could not find control in ControlParamaterASP.NET 在 ControlParamater 中找不到控件
【发布时间】:2012-02-12 16:14:59
【问题描述】:

在这一点上我很迷茫(已经工作了一段时间没有并且正在碰壁/截止日期)但是我被抛出的错误消息是在我点击 btnupdate 更新数据库中的字段之后。

完整的错误信息:

Could not find control 'txtTitle' in ControlParameter 'Title'.

页面:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<div class="floater">
<h3>Books</h3>
<asp:DropDownList
    id="DropDownList_Books"
    DataSourceId="srcBooks"
    DataTextField="Title"
    DataValueField="Id"
    Runat="server" />
    <asp:Button ID="btnSelect" runat="server" Text="View Detials" Width="106px" 
        onclick="btnSelect_Click" />
</div>

<asp:GridView
    id="grdBooks"
    DataSourceID="srcBooks_Description"
    Runat="server" Visible="False" />

    <asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />

</div>

<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click"
    Text="Cancel" Visible="False" />

<asp:FormView
    id="frmEditBook"
    DataKeyNames="Cat_Id"
    DataSourceId="srcBooks_Description"
    DefaultMode="Edit"
    Runat="server" Visible="False" 

    style="z-index: 1; left: 391px; top: 87px; position: absolute; height: 111px; width: 206px" >
    <EditItemTemplate>
    <asp:Label
        id="lblTitle"
        Text="Title:"
        AssociatedControlID="txtTitle"
        Runat="server" />
    <asp:TextBox
        id="txtTitle"
        Text='<%#Bind("Title")%>'
        Runat="server" />
    <br />
    <asp:Label
        id="lblDescription"
        Text="Description:"
        AssociatedControlID="txtDescription"
        Runat="server" />
    <asp:TextBox
        id="txtDescription"
        Text='<%#Bind("Description")%>'
        Runat="server" />
    <br />
    <asp:Button
        id="btnUpdate"
        Text="Update"
        CommandName="Update"
        Runat="server" />
    </EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="srcBooks" runat="server" 
    ConnectionString="Data Source=****;;Initial Catalog=***;Persist Security Info=True;User ID=****;Password=****" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT Title,Id FROM PARTIN_ID">

</asp:SqlDataSource>

<asp:SqlDataSource ID="srcBooks_Description" runat="server" 
    ConnectionString="Data Source=****, 14330&quot;;Initial Catalog=****;Persist Security Info=True;User ID=****;Password=****" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM PARTIN_INFO WHERE Cat_ID=@Id" UpdateCommand="UPDATE PARTIN_INFO SET Title=@Title,
        Description=@Description WHERE Cat_Id=@Id">
<SelectParameters>
        <asp:ControlParameter
            Name="Id"
            Type="int32"                
            ControlID="DropDownList_Books"
            PropertyName="SelectedValue" />
    </SelectParameters>
     <UpdateParameters>
          <asp:ControlParameter Name="Title" ControlId="txtTitle" PropertyName="Text"/>
          <asp:ControlParameter Name="Description" ControlId="txtDescription" PropertyName="Text"/>
          <asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
      </UpdateParameters>

</asp:SqlDataSource>

</form>
</body>
</html>

代码背后:

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void srcBooks_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }

    protected void btnSelect_Click(object sender, EventArgs e)
    {
        grdBooks.Visible = true;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        frmEditBook.Visible = false;
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        frmEditBook.Visible = true;
        btnCancel.Visible = true;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            srcBooks_Description.Update();
        }
        catch (Exception except)
        {
            // Handle the Exception.
        }
    }
    }
}

【问题讨论】:

  • 永远不要将连接字符串信息发布到您的数据库中。这只是在自找麻烦,而不是您目前遇到的问题.....
  • 感谢您编辑我的错误!

标签: asp.net controlparameter


【解决方案1】:

您可以-充分地参考控件。

您需要在更新参数的 controlid 值前面加上封闭视图的控件 ID(在本例中为 FormView 'frmEditBook')

将 SqlDataSource 的 UpdateParamters 部分替换为以下内容:

<UpdateParameters>
   <asp:ControlParameter Name="Title" ControlId="frmEditBook$txtTitle" PropertyName="Text"/>
   <asp:ControlParameter Name="Description" ControlId="frmEditBook$txtDescription" PropertyName="Text"/>
   <asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
</UpdateParameters>

注意

ControlId="DropDownList_Books" 

保持不变,因为此控件不在 FormView 的范围内。

我不建议使用浏览器提供的“全名”...特别是如果您使用母版页...它可能会随着您重新排列布局而改变。

【讨论】:

  • 我遇到了同样的问题,这对我有用,谢谢!
【解决方案2】:

如您所见,问题在于上下文。您还可以将您的 sqldatasource 放在被引用的控件所在的表单视图中(不正确且非常有限,但可以工作)

假设您有一个包含两个下拉列表的详细信息视图,其中一个依赖于另一个。对我有用的是将数据源放在与引用的控件相同的上下文(父控件)中,这将消除引用控件完整路径的需要。

我的两分钱是,这让人想起您必须如何使用子表单引用 Access Forms 中的控件。 :)

<asp:DetailsView runat="server">
    <asp:TemplateField>
        <EditTemplate>
          <asp:DropDownList id="ParentDDL" Datasource="SQLDataSource1">

          </asp:DropDownList>
        </EditTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <EditTemplate>
          <asp:DropDownList id="ParentDDL" Datasource="SQLDatasource2">

          </asp:DropDownList>
          <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * from table where field1=@field">
                                <SelectParameters>
                                    <asp:ControlParameter ControlID="ParentDDL" Name="field" 
                                        PropertyName="SelectedValue" Type="String" />
                                </SelectParameters>          
                            </asp:SqlDataSource>
        </EditTemplate>
    </asp:TemplateField>
</asp:DetailsView>

【讨论】:

    【解决方案3】:

    您需要使用其全名引用您的控件。

    当您在浏览器中打开页面时,您可以轻松找到控件的全名。只需查看源代码,您就会看到它。

    【讨论】:

      【解决方案4】:

      问题在于txtTitle 位于 FormView 内,因此无法以您尝试在 SqlDataSource 上执行的方式访问。您无法通过 ID 直接访问它。

      Here你有一篇 MSDN 文章可能有助于使用 FormView 和 SqlDataSource

      【讨论】:

      • 1.将其从表单视图中取出会解决问题还是会导致其他错误 2. Jesse 的建议是否可以作为快速修复?
      • 我建议您查看 MSDN 文章。这是处理这种情况的正确方法。
      【解决方案5】:

      您不应该使用&lt;asp:ControlParameter..,但&lt;asp:Parameter.. 和 SqlDataSource 控件在 bind 方法的帮助下足够智能,可以定位控件以获取参数值。

      以这种方式更新这部分 sqlDataSource:

        ....
       <UpdateParameters>
            <asp:Parameter Name="Title" Type="String"/>
            <asp:Parameter Name="Description" Type="String"/>
            <asp:Parameter Name="Id" Type="Int32"/>
        </UpdateParameters>
       .....
      

      请注意 Type 属性并提供相关类型。

      (问题是SqlDataSource控件不能显式访问数据绑定控件中定义的控件)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-09
        • 1970-01-01
        • 2011-03-05
        • 1970-01-01
        • 2011-12-14
        • 2011-09-28
        • 1970-01-01
        相关资源
        最近更新 更多