2012-02-12 162 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> 
相關問題