views:

153

answers:

2

Hello everyone

I am trying to make a page which allows me to delete job applicants from the db. My page works fine but whn i click on the delete button it reloads the page but doesnt delete the applicant and his details. I am using linq.

Here is my code

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

And Code behind:

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();
    }
}

Thanks...

A: 

Is it because you're not rebinding the event because you only populate your list (and therefore wire up your event on the buttons - I can't see where your wire-up is occurring) on a GET as opposed to a POST? Try removing the POstBack test to see if this is the case.

Program.X
hey thanks for replying.but could you be a little more specific.
I use the Repeater myself, was just an idea. Have you tried putting in the OnClick event in the button and binding there?eg: <asp:Button ID="ApplicantButton2" runat="server" OnClick="lv_Applicant_ItemDeleting" CommandName="Delete" Text="Delete" Width="50" />
Program.X
+1  A: 

Try to remove

EnableViewState=false"
Dmitris