2016-04-07 20 views
0

Werte zuzuweisen Ich versuche, einen Wert in TextBoxes in einem ListView entsprechend dem Benutzer-ID-Label-Wert des Schülers in der ListView einzufügen. Im Frontend gibt es ein Textfeld für jeden Schüler, der eine Note für. Unten ist ein Teil meines Code, den ich geschrieben habe, zu versuchen, das TextBox-Steuerelement zu finden, die Ergebnisse abzurufen, die bereits vorhanden sind und die Textfelder weisen Sie die richtigen Schüler passend:Versuch, Textboxen in Code hinter in ListView in ASP.NET

<div class="col-sm-12"> 
     <div class="row moduleDrop"> 

      <h2>Final module results</h2> 
      <h4>Select the degree programme:</h4> 
      <asp:Label ID="degreeProgValue" runat="server"></asp:Label> 
      <br /> 
      <asp:SqlDataSource ID="degreeSelect" runat="server" ConnectionString="<%$ ConnectionStrings:UniString %>" 
       SelectCommand="SELECT pathway_years.id, (pathway_year + ' ' + pathway) AS pathwayDetails FROM pathways INNER JOIN pathway_years ON pathways.id = pathway_years.pathway_id"></asp:SqlDataSource> 
      <asp:DropDownList ID="degreeProgDropDown" runat="server" Width="420" AutoPostBack="true" 
       DataSourceID="degreeSelect" DataTextField="pathwayDetails" DataValueField="id"> 
      </asp:DropDownList> 


     </div> 
    </div> 

    <div class="col-sm-12"> 
     <div class="row moduleDrop"> 
      <h4>Select module:</h4> 
      <asp:Label ID="TBTest" runat="server" Visible="true"></asp:Label> 
      <br /> 

      <asp:SqlDataSource ID="modSource" runat="server" ConnectionString="<%$ ConnectionStrings:UniString %>" 
       SelectCommand="SELECT DISTINCT module_on_pathway.module_id, modules.module_name FROM modules INNER JOIN module_on_pathway ON modules.id = module_on_pathway.module_id INNER JOIN pathway_years ON module_on_pathway.pathway_year_id = pathway_years.id INNER JOIN class ON pathway_years.id = class.pathway_year_id INNER JOIN classlist ON class.class_id = classlist.class_id INNER JOIN users ON classlist.user_id = users.id WHERE mandatory_module = 1 AND pathway_years.id = @pwayYearid UNION SELECT module_on_pathway.id, modules.module_name FROM modules INNER JOIN module_on_pathway ON modules.id = module_on_pathway.module_id INNER JOIN pathway_years ON module_on_pathway.pathway_year_id = pathway_years.id INNER JOIN class ON pathway_years.id = class.pathway_year_id INNER JOIN classlist ON class.class_id = classlist.class_id INNER JOIN chosen_modules_list ON classlist.classlist_id = chosen_modules_list.classlist_id INNER JOIN users ON classlist.user_id = users.id WHERE chosen_modules_list.module_on_pathway_id = module_on_pathway.id AND pathway_years.id = @pwayYearid"> 
       <SelectParameters> 
        <asp:ControlParameter ControlID="degreeProgDropDown" Name="pwayYearid" /> 
       </SelectParameters> 
      </asp:SqlDataSource> 
      <asp:DropDownList ID="modDropDown" runat="server" Width="420" AutoPostBack="true" EnableViewState="true" 
       DataSourceID="modSource" DataTextField="module_name" DataValueField="module_id"> 
      </asp:DropDownList> 
     </div> 
     <br /> 
    </div> 

    <div class="classList"> 

     <h4>Select the student:</h4> 
     <br /> 
     <asp:SqlDataSource ID="SQLStudentList" runat="server" ConnectionString="<%$ ConnectionStrings:UniString %>" 
      SelectCommand="SELECT students_profile.user_id, first_name, last_name FROM classlist 
     INNER JOIN students_profile ON classlist.user_id = students_profile.user_id 
     INNER JOIN class ON classlist.class_id = class.class_id 
     INNER JOIN pathway_years ON class.pathway_year_id = @pwayYearid 
     INNER JOIN module_on_pathway ON pathway_years.id = module_on_pathway.pathway_year_id 
     WHERE module_on_pathway.mandatory_module = 1 
     AND module_on_pathway.module_id = @modpwayid 

     UNION 
     SELECT students_profile.user_id, first_name, last_name FROM classlist 
     INNER JOIN students_profile ON classlist.user_id = students_profile.user_id 
     INNER JOIN class ON classlist.class_id = class.class_id 
     INNER JOIN pathway_years ON class.pathway_year_id = @pwayYearid 
     INNER JOIN module_on_pathway ON pathway_years.id = module_on_pathway.pathway_year_id 
     INNER JOIN chosen_modules_list ON classlist.classlist_id = chosen_modules_list.classlist_id 
     WHERE chosen_modules_list.module_on_pathway_id = module_on_pathway.id 
     AND module_on_pathway.module_id = @modpwayid"> 
      <SelectParameters> 
       <asp:ControlParameter ControlID="degreeProgDropDown" Name="pwayYearid" /> 
       <asp:ControlParameter Name="modpwayid" ControlID="modDropDown" /> 
      </SelectParameters> 
     </asp:SqlDataSource> 
     <asp:ListView runat="server" OnItemDataBound="studentsListView_ItemDataBound" ID="studentsListView" DataSourceID="SQLStudentList"> 
      <AlternatingItemTemplate> 
       <strong><span style="">Student Number:</strong> 
       <asp:Label ID="user_idLabel" runat="server" Text='<%# Eval("user_id") %>' /> 
       <br /> 
       <strong>Name:</strong> 
       <asp:Label ID="first_nameLabel" runat="server" Text='<%# Eval("first_name") %>' /> 
       <asp:Label ID="last_nameLabel" runat="server" Text='<%# Eval("last_name") %>' /> 
       <br /> 
       <asp:TextBox ID="finalMarkAssignment" runat="server" Width="40"></asp:TextBox> 
       <asp:Label ID="gradeForStudent" runat="server" Visible="false"></asp:Label> 
       <br /> 
       <br /> 
       <asp:Button CssClass="btn btn-success" ID="submitModuleMark" OnClientClick="ShowLabel();" runat="server" Text="Submit Grade" OnClick="submitModuleMark_Click" /> 
       <br /> 

       <asp:Button CssClass="btn btn-success" ID="repeatSubmitModuleMark" OnClientClick="ShowLabel();" runat="server" Text="Submit repeat module grade" OnClick="repeatSubmitModuleMark_Click" /> 
       <br /> 
       <br /> 
       </span> 
      </AlternatingItemTemplate> 
      <EditItemTemplate> 
       <span style="">user_id: 
        <asp:TextBox ID="user_idTextBox" runat="server" Text='<%# Bind("user_id") %>' /> 
        <br /> 
        first_name: 
        <asp:TextBox ID="first_nameTextBox" runat="server" Text='<%# Bind("first_name") %>' /> 
        <br /> 
        last_name: 
        <asp:TextBox ID="last_nameTextBox" runat="server" Text='<%# Bind("last_name") %>' /> 
        <br /> 
        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" /> 
        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" /> 
        <br /> 
        <br /> 
       </span> 
      </EditItemTemplate> 
      <EmptyDataTemplate> 
       <span>No data was returned.</span> 
      </EmptyDataTemplate> 
      <InsertItemTemplate> 
       <span style="">user_id: 
        <asp:TextBox ID="user_idTextBox" runat="server" Text='<%# Bind("user_id") %>' /> 
        <br /> 
        first_name: 
        <asp:TextBox ID="first_nameTextBox" runat="server" Text='<%# Bind("first_name") %>' /> 
        <br /> 
        last_name: 
        <asp:TextBox ID="last_nameTextBox" runat="server" Text='<%# Bind("last_name") %>' /> 
        <br /> 
        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /> 
        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" /> 
        <br /> 
        <br /> 
       </span> 
      </InsertItemTemplate> 
      <ItemTemplate> 
       <span style=""> 
        <strong>Student Number:</strong> 
        <asp:Label ID="user_idLabel" runat="server" Text='<%# Eval("user_id") %>' /> 
        <br /> 
        <strong>Name:</strong> 
        <asp:Label ID="first_nameLabel" runat="server" Text='<%# Eval("first_name") %>' /> 
        <asp:Label ID="last_nameLabel" runat="server" Text='<%# Eval("last_name") %>' /> 
        <br /> 
        <asp:TextBox ID="finalMarkAssignment" runat="server" Width="40"></asp:TextBox> 
        <br /> 
        <br /> 
        <asp:Button CssClass="btn btn-success" ID="submitModuleMark" OnClientClick="ShowLabel();" runat="server" Text="Submit Grade" OnClick="submitModuleMark_Click" /> 
        <br /> 

        <asp:Button CssClass="btn btn-success" ID="repeatSubmitModuleMark" OnClientClick="ShowLabel();" runat="server" Text="Submit repeat module grade" OnClick="repeatSubmitModuleMark_Click" /> 
        <br /> 
        <br /> 
       </span> 
      </ItemTemplate> 
      <LayoutTemplate> 
       <div id="itemPlaceholderContainer" runat="server" style=""> 
        <span runat="server" id="itemPlaceholder" /> 
       </div> 
       <div style=""> 
       </div> 
      </LayoutTemplate> 
      <SelectedItemTemplate> 
       <span style="">user_id: 
        <asp:Label ID="user_idLabel" runat="server" Text='<%# Eval("user_id") %>' /> 
        <br /> 
        first_name: 
        <asp:Label ID="first_nameLabel" runat="server" Text='<%# Eval("first_name") %>' /> 
        <br /> 
        last_name: 
        <asp:Label ID="last_nameLabel" runat="server" Text='<%# Eval("last_name") %>' /> 
        <br /> 
        <br /> 
       </span> 
      </SelectedItemTemplate> 
     </asp:ListView> 


    </div> 


    <div class="col-sm-12"> 

     <br /> 

     <asp:Label ID="changedFlag" runat="server" Visible="false"></asp:Label> 
     <asp:Label ID="assignmentIDValue" runat="server" Visible="false"></asp:Label> 
     <asp:Label ID="pOrF" runat="server" Visible="false"></asp:Label> 
     <asp:Label ID="EnterFinalMark" runat="server" Visible="false">Enter a final mark for this module</asp:Label> 
     <br /> 


     <asp:Label ID="success" runat="server" Visible="false" Text="Grade has been changed successfully"></asp:Label> 
     <asp:Label ID="changedVAL" Visible="false" runat="server"></asp:Label> 
    </div> 
</div> 

+0

Nicht verketten Strings für Ihre Abfragen. Benutzte parametrisierte Abfragen. – mason

Antwort

0

Wenn Sie einen Breakpoint auf TextBox TB setzen, hat es einen Wert, wenn Sie darauf einsteigen?

Ich vermute, dass Sie diesen Code nicht in eine Methode eingefügt haben, die auf Datenbindung für die Listview-Elemente ausgelöst wird, und Sie müssen. Wenn Sie es an anderer Stelle (z. B. beim Laden der Seite) einfügen, kann es die einzelnen Textfelder nicht finden.

Zuerst müssen Sie das ItemDataBound-Ereignis der ListView in Ihrem Seitenladecode abonnieren. Sie können zu diesem Zweck entweder deklarativ in Front-End-Code

<asp:ListView ID="studentsListView" OnItemDataBound="listView_OnItemDataBound" runat="server"></asp:ListView> 

Oder Sie können im Backend Code

studentsListView.ItemDataBound += listView_OnItemDataBound(); 

Und dann für jedes Element in der Seite zu laden tun, können Sie etwas tun damit

protected void listView_OnItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    // get the button or any other control on the current row 
    TextBox tB = (TextBox) e.Item.FindControl("finalMarkAssignment"); 
    if(tb != null) 
    { 
    // do something 
    } 
} 
+0

Was wäre im Idealfall die Methode, um diesen Code zu haben? Ich bin ein Neuling mit solchen Problemen. – Spiros

+0

Ich habe jetzt meinen Code in einem OnItemDataBound Event aber immer noch kein Glück. Ich werde meine Frage mit dem Code aktualisieren, den ich jetzt habe – Spiros

+0

Ist TB null oder hat es einen Wert, wenn Sie einen Haltepunkt darauf setzen? Wenn Sie Ihre Änderungen veröffentlichen möchten, empfehle ich Ihnen dringend, jeden fremden Code zu entfernen, der nicht direkt mit Ihrem Problem zusammenhängt. Dadurch wird es schwierig, das Problem zu erkennen, das Sie haben. – Jonathan

Verwandte Themen