2012-02-12 4 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

+0

저의 나쁜 것을 고맙다는 것에 감사드립니다! – user1062411

답변

0

.

브라우저에서 페이지를 열면 컨트롤의 전체 이름을 쉽게 찾을 수 있습니다. 소스보기 만하면 볼 수 있습니다.

0

txtTitle은 FormView 내부에 있으므로 SqlDataSource에서 수행하려고 시도하는 방식으로는 액세스 할 수 없습니다. ID로 direclty에 액세스 할 수 없습니다.

Here

당신은 FormView와의 SqlDataSource

+0

다음 두 가지 질문이 있습니다 : – user1062411

+0

1. 폼뷰에서 문제를 해결하거나 다른 오류가 발생할 수 있습니다. 2. Jesse는 어떤 작업을 빠른 수정으로 제안 했습니까? – user1062411

+0

MSDN 기사를 살펴 보는 것이 좋습니다. 그것은이 상황을 다루기위한 적절한 방법입니다. –

0

당신이 <asp:ControlParameter..하지만 <asp:Parameter.. 사용하지 말았어야 및 바인드 방법의 도움으로 SqlDataSource 컨트롤 작동하도록하는 데 도움이 수있는 MSDN 문서를 찾을 수있을만큼 지능이 매개 변수에 대한 값을 가져 오는 컨트롤

업데이트의 SqlDataSource의이 부분이 방법 :

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

유형 특성을 참고하고 관련 유형을 제공하십시오.

당신은 적절하게 컨트롤을 참조 -can-

4

(문제는 명시 적으로 데이터 바인딩 된 컨트롤에 정의 된 컨트롤에 액세스 할 수 없습니다 SqlDataSource 컨트롤)입니다.

:

은 다음으로의 SqlDataSource의 UpdateParamters 부분을 교체하여 바깥보기 (이 경우 FormView 'frmEditBook')

의 제어 ID로 업데이트 매개 변수의 컨트롤의 controlid 값을 접두사 필요 이 컨트롤은 FormView의 범위 내에 있지으로

ControlId="DropDownList_Books" 

가 동일하게 유지

<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> 

참고.

브라우저에서 제공하는 '전체 이름'사용을 권장하지 않습니다 ... 특히 마스터 페이지를 사용하는 경우 레이아웃을 다시 배열 할 때 변경 될 수 있습니다.

+0

나는 똑같은 문제가 있었고이 일은 저에게 감사합니다! – Andriusha

1

보시다시피, 문제는 문맥입니다. 또한 참조되는 컨트롤이 (적절하고 매우 제한적이지만 작동하지 않을 수도있는) formview 내에 sqldatasource를 넣을 수 있습니다.

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>