Asked by:
Listview Insert multiple records

Question
-
User444756084 posted
I have a list view that based on the hrs will populate with a second row (the special row in the code below). I am trying to insert both rows with one submit button. However, it is not working. Any advice on how to get this to submit?
[System.Web.Services.WebMethod] protected void ListView3_ItemInserting(object sender, ListViewInsertEventArgs e) { DataTable prevcrse = (DataTable)ViewState["DeptAction"]; ListView lv = (ListView)Wizard1.FindControl("ListView3"); //GridView3.DataBind(); DropDownList dept = (DropDownList)lv.InsertItem.FindControl("ddlSubject"); DropDownList cno = (DropDownList)lv.InsertItem.FindControl("ddlClass"); TextBox ctl = (TextBox)lv.InsertItem.FindControl("CourseTitleTextBox"); TextBox hrs = (TextBox)lv.InsertItem.FindControl("HrsTextBox"); TextBox comments = (TextBox)lv.InsertItem.FindControl("CommentsTextBox"); CheckBox add = (CheckBox)lv.InsertItem.FindControl("CheckBox1"); //add special row DropDownList dept2 = (DropDownList)lv.InsertItem.FindControl("AddIfHoursLessSubjectDD"); DropDownList cno2 = (DropDownList)lv.InsertItem.FindControl("AddIfHoursLessClassDD"); TextBox hrs2 = (TextBox)lv.InsertItem.FindControl("AddIfHoursLessSubjectHrs"); TextBox comments2 = (TextBox)lv.InsertItem.FindControl("TextBox4"); CheckBox add2 = (CheckBox)lv.InsertItem.FindControl("CheckBox2"); string sourceid = string.Empty; if (ViewState["SourceID"] != null) { sourceid = ViewState["SourceID"].ToString(); } DataRow row = prevcrse.NewRow(); int i = prevcrse.Rows.Count + 1; if (prevcrse.Rows.Count > 0) { DataView dv = prevcrse.DefaultView; foreach (DataRow dr in dv.Table.Rows) { int j = Convert.ToInt32(dr["RowNumber"].ToString()); if (j == i) { i++; } } } //displays submitted data row["RowNumber"] = i.ToString(); row["Dept"] = dept.SelectedValue.ToString(); row["CourseNo"] = cno.SelectedValue.ToString(); row["Hrs"] = hrs.Text.ToString(); row["Comments"] = comments.Text.ToString(); row["Add"] = add.Checked.ToString(); //special row row["Dept"] = dept2.SelectedValue.ToString(); row["CourseNo"] = cno2.SelectedValue.ToString(); row["Hrs"] = hrs2.Text.ToString(); row["Comments"] = comments2.Text.ToString(); row["Add"] = add2.Checked.ToString(); prevcrse.Rows.Add(row); DataView dv2 = (DataView)prevcrse.DefaultView; lv.DataSource = dv2; ViewState["DeptAction"] = prevcrse; lv.DataBind(); if (Session["IPhere"] != null) { if (Session["IPhere"].ToString() == "true") { CheckBox add1 = (CheckBox)ListView3.InsertItem.FindControl("CheckBox1"); add1.Enabled = false; } } }
Tuesday, October 1, 2019 7:51 PM
All replies
-
User288213138 posted
Hi gordon1221,
I am trying to insert both rows with one submit button. However, it is not workingAccording to the code you provided, I can't reproduce your question.
I don't know how you construct your aspx page and what is ViewState["DeptAction"] etc.
You can debug through the breakpoint to find out where the problem is.
If you can't solve the question, please post more codes about your question.
Best regards,
Sam
Wednesday, October 2, 2019 5:53 AM -
User444756084 posted
Hello,
below is my listview code and jquery that populates the special row. What I am trying to do is if the special row displays then I need them both to insert.:
<asp:ListView ID="ListView3" runat="server" InsertItemPosition="LastItem" DataKeyNames="RowNumber" OnItemInserting="ListView3_ItemInserting" OnItemDeleting="ListView3_ItemDeleting" OnItemEditing="ListView3_ItemEditing" OnItemUpdating="ListView3_ItemUpdating" OnItemCanceling="ListView3_ItemCanceling" OnItemDataBound="ListView3_ItemDataBound"> <AlternatingItemTemplate> <tr style=""> <td> <asp:Label ID="DeptLabel" runat="server" Text='<%# Eval("Dept") %>' /> </td> <td> <asp:Label ID="CourseNoLabel" runat="server" Text='<%# Eval("CourseNo") %>' /> </td> <td> <asp:Label ID="HrsLabel" runat="server" Text='<%# Eval("Hrs") %>' /> </td> <td> <asp:Label ID="CreditDeptLabel" runat="server" Text='<%# Eval("Comments") %>' /> </td> <td> <asp:Label ID="GradeLabel" runat="server" Text='<%# Eval("Add") %>' /> </td> <td><asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /> </td> </tr> </AlternatingItemTemplate> <EditItemTemplate> <tr style=""> <td> <asp:DropDownList runat="server" ID="ddlSubject" DataSourceID="sdsSubjects" DataTextField="Name" DataValueField="Subject" AutoPostBack="true" ToolTip="Select the Department." SelectedValue ='<%# Bind("Dept") %>'/> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Department required" Display="Dynamic" ValidationGroup="Update" ControlToValidate="ddlSubject" Font-Bold="True" ForeColor="#CC0000"></asp:RequiredFieldValidator> <asp:SqlDataSource runat="server" ID="sdsSubjects" ConnectionString="<%$ ConnectionStrings:OUR_Transfer_CreditConnectionString %>" SelectCommand="GetDepartments" SelectCommandType="StoredProcedure"> </asp:SqlDataSource> </td> <td> <asp:DropDownList runat="server" ID="ddlClass" DataSourceID="sdsClasses" DataTextField="Name" DataValueField="CATALOG_NBR" AutoPostBack="true" ToolTip="Select the Class." SelectedValue ='<%# Bind("CourseNo") %>' /><asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="Class number required" Display="Dynamic" ValidationGroup="Update" ControlToValidate="ddlClass" Font-Bold="True" ForeColor="#CC0000" ></asp:RequiredFieldValidator> <asp:SqlDataSource runat="server" ID="sdsClasses" ConnectionString="<%$ ConnectionStrings:OUR_Transfer_CreditConnectionString %>" SelectCommand="GetCourseNumber" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter Name="Subject" ControlID="ddlSubject" Type="String" /> </SelectParameters> </asp:SqlDataSource> </td> <td> <asp:TextBox ID="HrsTextBox" runat="server" ReadOnly="true" Text='<%# Bind("Hrs") %>' /> <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Hours required" Display="Dynamic" ValidationGroup="Update" ControlToValidate="HrsTextBox" Font-Bold="True" ForeColor="#CC0000"></asp:RequiredFieldValidator> </td> <td> <asp:TextBox ID="CommentsTextBox" runat="server" Text='<%# Bind("Comments") %>' MaxLength="250" /> <%-- <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="Comments required" Display="Dynamic" ValidationGroup="Update" ControlToValidate="CommentsTextBox" Font-Bold="True" ForeColor="#CC0000" Enabled="false"></asp:RequiredFieldValidator>--%> </td> <td> <asp:CheckBox ID="CheckBox1" runat="server" Text="" Checked="true" /> </td> <td> <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" CausesValidation="true" ValidationGroup="Update"/> <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="true" ValidationGroup="Delete"/> <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" /> </td> </tr> </EditItemTemplate> <EmptyDataTemplate> <table runat="server" class="table table-striped table-bordered table-hover"> <tr> <td>No data was returned.</td> </tr> </table> </EmptyDataTemplate> <InsertItemTemplate> <tr style=""> <td> <asp:DropDownList runat="server" ID="ddlSubject" DataSourceID="sdsSubjects" DataTextField="Name" DataValueField="Subject" AutoPostBack="true" ToolTip="Select the Department." /> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Department required" Display="Dynamic" ValidationGroup="AddEval" ControlToValidate="ddlSubject" Font-Bold="True" ForeColor="#CC0000"></asp:RequiredFieldValidator> <asp:SqlDataSource runat="server" ID="sdsSubjects" ConnectionString="<%$ ConnectionStrings:OUR_Transfer_CreditConnectionString %>" SelectCommand="GetDepartments" SelectCommandType="StoredProcedure"> </asp:SqlDataSource> </td> <td> <asp:DropDownList runat="server" ID="ddlClass" DataSourceID="sdsClasses" DataTextField="Name" DataValueField="CATALOG_NBR" AutoPostBack="true" ToolTip="Select the Class." onselectedindexchanged="ddlClass_SelectedIndexChanged" /><asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="Class number required" Display="Dynamic" ValidationGroup="AddEval" ControlToValidate="ddlClass" Font-Bold="True" ForeColor="#CC0000"></asp:RequiredFieldValidator> <asp:SqlDataSource runat="server" ID="sdsClasses" ConnectionString="<%$ ConnectionStrings:OUR_Transfer_CreditConnectionString %>" SelectCommand="GetCourseNumber" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter Name="Subject" ControlID="ddlSubject" Type="String" /> </SelectParameters> </asp:SqlDataSource> </td> <td> <asp:TextBox ID="HrsTextBox" runat="server" Width="50px" class="qty" ReadOnly="true" Text='<%# Bind("Hrs") %>' placeholder="Hours" /> <%-- <asp:SqlDataSource runat="server" ID="sdsCourseHrs" ConnectionString="<%$ ConnectionStrings:OUR_Transfer_CreditConnectionString %>" SelectCommand="GetCourseHrs" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter Name="Subject" ControlID="ddlSubject" Type="String" /> <asp:ControlParameter Name="CATALOG_NBR" ControlID="ddlClass" Type="String" /> </SelectParameters> </asp:SqlDataSource>--%> </td> <td> <asp:TextBox ID="CommentsTextBox" runat="server" Text='<%# Bind("Comments") %>' MaxLength="250" placeholder="Comments here" /> <%-- <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="Comments required" Display="Dynamic" ValidationGroup="AddEval" ControlToValidate="CommentsTextBox" Font-Bold="True" ForeColor="#CC0000" ></asp:RequiredFieldValidator>--%> </td> <td> <asp:CheckBox ID="CheckBox1" runat="server" Text="" Checked="true" /> </td> <td> <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Add" ValidationGroup="AddEval" /> <%-- <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />--%> </td> </tr> <tr id="AddIfHoursLess"style="background-color:lightgray"> <td> <asp:DropDownList runat="server" ID="AddIfHoursLessSubjectDD" DataSourceID="sdsSubjects" DataTextField="Name" DataValueField="Subject" AutoPostBack="true" ToolTip="Select the Department." /> <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="Department required" Display="Dynamic" ValidationGroup="AddEval" ControlToValidate="ddlSubject" Font-Bold="True" ForeColor="#CC0000"></asp:RequiredFieldValidator> <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:OUR_Transfer_CreditConnectionString %>" SelectCommand="GetDepartments" SelectCommandType="StoredProcedure"> </asp:SqlDataSource> </td> <td> <asp:DropDownList runat="server" ID="AddIfHoursLessCourseDD" DataSourceID="sdsClasses" DataTextField="Name" DataValueField="CATALOG_NBR" AutoPostBack="true" ToolTip="Select the Class." onselectedindexchanged="ddlClass_SelectedIndexChanged" /><asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ErrorMessage="Class number required" Display="Dynamic" ValidationGroup="AddEval" ControlToValidate="ddlClass" Font-Bold="True" ForeColor="#CC0000"></asp:RequiredFieldValidator> <asp:SqlDataSource runat="server" ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:OUR_Transfer_CreditConnectionString %>" SelectCommand="GetCourseNumber" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter Name="Subject" ControlID="ddlSubject" Type="String" /> </SelectParameters> </asp:SqlDataSource> </td> <td> <asp:TextBox ID="AddIfHoursLessSubjectHrs" runat="server" Width="50px" ReadOnly="true" Text='<%# Bind("Hrs") %>' placeholder="Hours" /> </td> <td> <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Comments") %>' MaxLength="250" placeholder="Comments here" /> <%-- <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" ErrorMessage="Comments required" Display="Dynamic" ValidationGroup="AddEval" ControlToValidate="CommentsTextBox" Font-Bold="True" ForeColor="#CC0000" ></asp:RequiredFieldValidator>---%> </td> <td> <asp:CheckBox ID="CheckBox2" runat="server" Text="" Checked="true" /> </td> <td> <asp:Button ID="Button5" runat="server" CommandName="Insert" Text="Add" ValidationGroup="AddEval" /> <%-- <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />--%> </td> </tr> </InsertItemTemplate> <ItemTemplate> <tr style=""> <td> <asp:Label ID="DeptLabel" runat="server" Text='<%# Eval("Dept") %>' /> </td> <td> <asp:Label ID="CourseNoLabel" runat="server" Text='<%# Eval("CourseNo") %>' /> </td> <td> <asp:Label ID="HrsLabel" runat="server" Text='<%# Eval("Hrs") %>' /> </td> <td> <asp:Label ID="CreditDeptLabel" runat="server" Text='<%# Eval("Comments") %>' /> </td> <td> <asp:Label ID="GradeLabel" runat="server" Text='<%# Eval("Add") %>' /> </td> <td> <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /> </td> </tr> </ItemTemplate> <LayoutTemplate> <table runat="server"> <tr runat="server"> <td runat="server"> <table id="itemPlaceholderContainer" runat="server" class="table table-striped table-bordered table-hover"> <tr runat="server" style=""> <th runat="server">Dept</th> <th runat="server">Course#</th> <th runat="server">Hrs</th> <th runat="server">Comment</th> <th runat="server">Add to Articulation Table</th> <th runat="server"></th> </tr> <tr id="itemPlaceholder" runat="server"> </tr> </table> </td> </tr> <tr runat="server"> <td runat="server" style=""></td> </tr> </table> </LayoutTemplate> <SelectedItemTemplate> <tr style=""> <td> <asp:Label ID="DeptLabel" runat="server" Text='<%# Eval("Dept") %>' /> </td> <td> <asp:Label ID="CourseNoLabel" runat="server" Text='<%# Eval("CourseNo") %>' /> </td> <td> <asp:Label ID="HrsLabel" runat="server" Text='<%# Eval("Hrs") %>' /> </td> <td> <asp:Label ID="CreditDeptLabel" runat="server" Text='<%# Eval("Comments") %>' /> </td> <td> <asp:Label ID="GradeLabel" runat="server" Text='<%# Eval("Add") %>' /> </td> <td> </td> </tr> </SelectedItemTemplate> </asp:ListView> <script src="js/jquery-1.12.0.min.js"></script> <script> $(document).ready(function () { // Initially, Hide when Web Form is loaded $('#IPDeferredInputTR, #AddIfHoursLess').hide(); $('#ContentPlaceHolder1_Wizard1_ListView1_IPDeferredCB').change(function () { if ($(this).prop('checked')) { $('#IPDeferredInputTR').show(); } else { $('#IPDeferredInputTR').hide(); } }); $('#ContentPlaceHolder1_Wizard1_ListView3_CheckBox1').change(function () { if ($(this).is(':checked')) { $('#ContentPlaceHolder1_Wizard1_ListView3_CommentsTextBox').prop('required', false); $('#ContentPlaceHolder1_Wizard1_ListView3_RequiredFieldValidator9').attr("disabled", true).hide(); } else { $('#ContentPlaceHolder1_Wizard1_ListView3_CommentsTextBox').prop('required', true); $('#ContentPlaceHolder1_Wizard1_ListView3_RequiredFieldValidator9').attr("disabled", false).show(); } }); // function createSpecialRows() { $('#ContentPlaceHolder1_Wizard1_ListView3_ddlSubject').change(function (e) { var subject = $("#ContentPlaceHolder1_Wizard1_ListView3_ddlSubject:nth-child(1) option:selected").val(); // alert(subject); $("#ContentPlaceHolder1_Wizard1_ListView3_AddIfHoursLessSubjectDD").val(subject).attr('selected', 'true'); }); //sets Course# = 'Special' $('#ContentPlaceHolder1_Wizard1_ListView3_ddlClass').change(function (e) { //var course = $("#ContentPlaceHolder1_Wizard1_ListView3_ddlClass:nth-child(1) option:selected").val(); // alert(course); $("#ContentPlaceHolder1_Wizard1_ListView3_AddIfHoursLessCourseDD").val('Special').attr('selected', 'true'); }); var hrsExistYet = $('.qty').val(); alert("hrsexistYet " + hrsExistYet); if (hrsExistYet > 0) { showSpecialRow(); } //showSpecialRow(); function showSpecialRow() { alert("Im in the showSpecial function"); var getOSUVal = $('.getHrsVal').text(); //Get OSU Hrs to compare alert(getOSUVal); var sum = 0; $(".qty").each(function () { //sum all hours sum += $(this).val(); }); alert("Sum " + sum); if (getOSUVal > sum) { alert("In the if"); var SpecialHrs = (getOSUVal - sum).toFixed(2); $("#ContentPlaceHolder1_Wizard1_ListView3_AddIfHoursLessSubjectHrs").val(SpecialHrs); } else { alert("not in the if"); } $('#AddIfHoursLess').show(); $('#ContentPlaceHolder1_Wizard1_ListView3_InsertButton').hide(); }; //$('#ContentPlaceHolder1_Wizard1_ListView3_InsertButton').click(function () { // createSpecialRows(); //}); }); </script>
Wednesday, October 2, 2019 8:07 PM -
User288213138 posted
Hi gordon1221,
below is my listview code and jquery that populates the special row. What I am trying to do is if the special row displays then I need them both to insert.:Your code says that if the value of HrsTextBox >0, it will display special row. then you want to instert these 2 rows via the Add button.
The problem you are having is that you can only insert one line of code? (Due to the lack of part of the code, I am not sure if it reproduces your question)
If so, I suggest that you can debug with breakpoint to view some values. such as: dv, dv2. So you can find the problem.
if not, post more details information about your problem.
Best regards,
Sam
Friday, October 4, 2019 12:03 PM