【问题标题】:Delete function skipped in asp.net在 asp.net 中跳过的删除功能
【发布时间】:2010-10-17 15:39:47
【问题描述】:

我正在尝试创建一个页面,允许我从数据库中删除求职者。 我的页面工作正常,但是当我单击删除按钮时,它会重新加载页面,但不会删除申请人及其详细信息。我正在使用 linq。

这是我的代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ApplicantManagement.aspx.cs" Inherits="AdminPanel_ApplicantManagement" MasterPageFile="../AdminMaster.master" Title="ApplicantManagement" EnableEventValidation="false" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPagePlaceHolder1" Runat="Server">
<div class="content"> 
    <div class="titleMain">Library -> Applicant Management
    </div>
    <h1><span></span>Applicant Management</h1>

    <div class="contentpage" >
        <p>On this page you add, update, delete the Job applicants.
        <b><asp:Label ID="ApplicantError" runat="server" Visible="false" /></b>


        <asp:Label ID="ApplicantMsg" runat="server" Visible="false" /> 
        <br />
        <table>
              <tr>
              <td>
                <asp:DropDownList ID="JobList" runat="server">
                </asp:DropDownList>
             </td>
             <td>
                <asp:Button ID="select" runat="server"  Text="Select"  onclick="btn_Select_Click"/>
             </td>
             </tr>
        </table>     

        <asp:ListView ID="lv_Applicant" runat="server" DataKeyNames="applicant_id" 
             EnableViewState="false" OnItemDeleting="lv_Applicant_ItemDeleting">             
        <LayoutTemplate>
               <table width="550" class="table">
                    <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
                </table> 
                <asp:DataPager ID="ApplicantPager" runat="server" PageSize="5">
                    <Fields>
                        <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" />
                        <asp:NumericPagerField />
                        <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="false" ShowNextPageButton="true" ShowLastPageButton="false" />
                    </Fields>
                </asp:DataPager>
            </LayoutTemplate>
                <ItemTemplate>
                    <tr>
                        <td></td> 
                        <td><asp:Label ID="ApplicantID" runat="server" Text='<%# Eval("applicant_id") %>' Visible="false" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Name</b>
                        </td>
                        <td>
                            <asp:Label ID="ApplicantName" runat="server" Text='<%# Eval("applicant_name") %>'/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Address</b>
                        </td>
                        <td>
                            <asp:Label ID="ApplicantAddress" runat="server" Text='<%# Eval("applicant_address") %>'/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Phone</b>
                        </td>
                        <td>
                            <asp:Label ID="ApplicantPone" runat="server" Text='<%# Eval("applicant_phone") %>'/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Email</b>
                        </td>
                        <td>
                           <asp:Label ID="ApplicantEmail" runat="server" Text='<%# Eval("applicant_email") %>'/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                           <b>Education</b> 
                        </td>
                        <td>
                           <asp:Label ID="ApplicantEducation" runat="server" Text='<%# Eval("applicant_education") %>'/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                             <b>Experience</b>
                        </td>
                        <td>
                            <asp:Label ID="ApplicantExperience" runat="server" Text='<%# Eval("applicant_experience") %>'/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Interests</b>
                        </td>
                        <td>
                           <asp:Label ID="ApplicantInterests" runat="server" Text='<%# Eval("applicant_interest") %>'/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Other</b>
                        </td>
                        <td>
                            <asp:Label ID="ApplicantOther" runat="server" Text='<%# Eval("applicant_other") %>'/>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>

                            <asp:Button ID="ApplicantButton2" runat="server" CommandName="Delete"
                              Text="Delete" Width="50" /><br /><br />
                        </td>
                    </tr>
                </ItemTemplate>      
        </asp:ListView> 
    </p></div>
</div>      
</asp:Content>

以及背后的代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class AdminPanel_ApplicantManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getApplicantListDDL();
        }

    }

    #region Getting the List
    protected void getApplicantListDDL()
    {
        using (CareersDataContext applist = new CareersDataContext())
        {
            var appList = from a in applist.team5_jobs
                             select new
                             {
                                 a.job_id,
                                 a.job_name
                             };
            JobList.DataSource = appList;
            JobList.DataTextField = "job_name";
            JobList.DataValueField = "job_id";
            JobList.DataBind();
            JobList.Items[0].Selected = true;
        }
    }
    #endregion

    protected void lv_Applicant_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int ID = Int32.Parse(lv_Applicant.DataKeys[e.ItemIndex].Value.ToString());
        using (CareersDataContext applist = new CareersDataContext())
        {
            team5_job_applicant applicants = applist.team5_job_applicants.Single(al => al.applicant_id == ID);
            applist.team5_job_applicants.DeleteOnSubmit(applicants);
            applist.SubmitChanges();         
        }

        getApplicantList();
    }


    protected void getApplicantList()
    {
        string jobName = JobList.SelectedItem.ToString();
        using (CareersDataContext applist = new CareersDataContext())
        {
            var applicantListVar = from apl in applist.team5_job_applicants
                            where (apl.job_name == jobName)
                            select new
                            {
                                job_name = apl.job_name,
                                applicant_id = apl.applicant_id,
                                applicant_name = apl.applicant_name,
                                applicant_address = apl.applicant_address,
                                applicant_phone = apl.applicant_phone,
                                applicant_email = apl.applicant_email, 
                                applicant_education = apl.applicant_education,
                                applicant_experience = apl.applicant_experience,
                                applicant_interest = apl.applicant_interest,
                                applicant_other = apl.applicant_other
                            };

            lv_Applicant.DataSource = applicantListVar;
            lv_Applicant.DataBind();
        }
    }

    protected void btn_Select_Click(object sender, EventArgs e)
    {
        getApplicantList();
    }
}

谢谢...

【问题讨论】:

    标签: asp.net linq .net-3.5


    【解决方案1】:

    是不是因为您没有重新绑定事件,因为您只是在 GET 上填充列表(因此将您的事件连接到按钮上 - 我看不到您的连接发生在哪里)在 GET 而不是邮政?尝试删除 POstBack 测试,看看是否是这种情况。

    【讨论】:

    • 您好,感谢您的回复。但你能说得更具体一点吗?
    • 我自己使用中继器,只是一个想法。您是否尝试在按钮中放入 OnClick 事件并在那里绑定?例如:
    【解决方案2】:

    尝试删除

    EnableViewState=false"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多