2012-02-12 36 views
0

此时我很迷茫(一直在使用它一段时间,而且正在敲墙/最后期限),但是我正在抛出的错误消息是在我点击btnupdate更新数据库中的字段。ASP.NET无法在ControlParamater中找到控件

完整的错误信息:

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 [email protected]" UpdateCommand="UPDATE PARTIN_INFO SET [email protected], 
     [email protected] WHERE [email protected]"> 
<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. 
     } 
    } 
    } 
} 
+1

切勿将连接字符串信息发布到您的数据库。除了你目前遇到的问题,这只是要求麻烦..... – Jesse 2012-02-12 16:19:54

+0

感谢您编辑我的坏! – user1062411 2012-02-12 16:29:52

回答

0

您需要参考其全名你的控制。

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

0

问题是txtTitle在FormView中,所以它不能以您想要在SqlDataSource上执行的方式访问。您无法通过ID直接访问它。

Here你有一个MSDN文章可能有助于与你不应该使用<asp:ControlParameter..<asp:Parameter..一个FormView和SqlDataSource的

+0

然后有两个问题: – user1062411 2012-02-12 16:30:38

+0

1.将它从一个formview解决问题或虽然导致其他错误 2.杰西建议工作作为一个快速解决? – user1062411 2012-02-12 16:31:38

+0

我建议你看看MSDN文章。这是处理这种情况的正确方法。 – 2012-02-12 16:55:01

0

,并与绑定方法的帮助下SqlDataSource控件的工作是足够的智能来定位控件从参数中获取值。

更新的SqlDataSource这一部分是这样的:

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

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

(问题是SqlDataSource控件不能明确地访问在数据绑定控件定义的控制)

4

您-can-参考控制充分。

你需要与你的封闭视图(FormView控件“frmEditBook”在这种情况下)

的控制ID前缀的更新参数的控件ID值与以下替换您的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控件的范围内。

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

+0

我有同样的问题和这项工作对我来说,谢谢! – Andriusha 2015-11-06 17:37:14

1

正如您所看到的,问题在于上下文。你也可以把你的sqldatasource放在控件引用的formview里面(不正确和非常有限,但可以工作)

假设你有一个detailsview,里面有两个下拉菜单,一个依赖于另一个下拉菜单。对我而言,最有效的方法是将数据源放在与控件相同的上下文(父控件)中,这样就不需要引用控件的完整路径。

我的两分钱是,这是非常让人想起你如何必须在子窗体访问窗体中引用控件。 :)

<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 [email protected]"> 
           <SelectParameters> 
            <asp:ControlParameter ControlID="ParentDDL" Name="field" 
             PropertyName="SelectedValue" Type="String" /> 
           </SelectParameters>   
          </asp:SqlDataSource> 
     </EditTemplate> 
    </asp:TemplateField> 
</asp:DetailsView> 
相关问题