单击事件不触发动态链接按钮

时间:2013-08-13 06:14:02

标签: c# html asp.net

我正在我的应用程序中创建一个Job Listing页面。我有一个要求,例如求职者点击查找工作按钮,列出了工作。我正在创建用于显示作业的动态表。我为求职者的每个表都有一个动态链接按钮,用于申请工作

aspx页面

   <script type="text/javascript">
        function RedirectTo(id) {
            window.location.href = 'ApplyJobsByCandidate.aspx?id=' + id;
            return false;
        }
</script>

   <table>
                 <tr>
                     <td>
                         <asp:DropDownList ID="ddlFilter" runat="server" CssClass="searchMainbtn" ForeColor="White" OnSelectedIndexChanged="ddlFilter_SelectedIndexChanged" AutoPostBack="true">
                         </asp:DropDownList>                    

                     </td>
                     <td>
                         <asp:Button ID="btnFindJobs" runat="server" Text="Find Jobs" CssClass="searchMainbtn" Width="103px" OnClick="btnFindJobs_Click" />
                     </td>
                     <td>
                         <asp:Label runat="server" Width="388px" ></asp:Label>
                     </td>
                     <td>
                          <asp:Label runat="server" ID="lblName" ForeColor="Crimson" Width="205px"></asp:Label>
                     </td>
                     <td>
                         <asp:LinkButton runat="server" ID="lbLogOut" Text="Log Out" OnClick="lbLogOut_Click" ForeColor="White" Font-Underline="false" CssClass="searchMainbtn"  Width="103px" Height="20px"></asp:LinkButton>
                     </td>
                 </tr>
             </table>

<div> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder></div>

aspx.cs页面

   if (!Page.IsPostBack)
                {
                    fillDropDownList();

                    lblError.Visible = false;
                    hlError.Visible = false;        
                }

                if (Page.IsPostBack)
                {

                    lblError.Visible = false;
                    hlError.Visible = false;        
                    industryName = ddlFilter.SelectedItem.Text.ToString();
                    lblJobName.Text = industryName.ToUpper() + " JOBS";
                    this.Rows = getTableRows();
                    this.Columns = Int32.Parse("1");       
                }
public void fillDropDownList()
{
    SqlDataAdapter da = new SqlDataAdapter("select * from [OfinityJobSearch].[dbo].[tm_TargetedIndustry]", con);
    DataTable dt = new DataTable();
    da.Fill(dt);
    ddlFilter.DataSource = dt;
    ddlFilter.DataTextField = "s_IndustryName";
    ddlFilter.DataValueField = "s_ID";
    ddlFilter.DataBind();
    ddlFilter.Items.Insert(0, "Filter Jobs");
    da.Dispose();
    con.Close();
}

protected void btnFindJobs_Click(object sender, EventArgs e)
    {

        this.Rows = getTableRows();
        this.Columns = Int32.Parse("1");
        if (this.Rows == 0)
        {
            CreateANullTable();
        }
        else
        {
            PlaceHolder1.Controls.Clear();
             getJobAdsBasedonFilter();
        }

    }

  public void getJobAdsBasedonFilter()
  {

    SqlCommand cmd = new SqlCommand();
    int ID = 0;

    try
    {
        cmd.Connection = con;

        cmd.CommandText = "SELECT TOP(10) s_JobDesignation,s_JobDescription,s_NoOfVacancies,s_DatePosted,s_JobId FROM [OfinityJobSearch].[dbo].[tx_ListOfJobs] WHERE s_IndustryName='" + industryName + "' ORDER BY s_JobId ASC ";
        cmd.CommandType = CommandType.Text;


        if (cmd.Connection.State == ConnectionState.Closed)
            cmd.Connection.Open();

        using (SqlDataReader reader = cmd.ExecuteReader())    
        {              
            if (reader.HasRows)
            {                    
                while (reader.Read())
                {    
                    JobDesignation = reader.GetString(0);
                    JobDescription = reader.GetString(1);
                    NoOfVacancies = Convert.ToString(reader.GetInt32(2));
                    DatePosted = Convert.ToString(reader.GetDateTime(3)).Replace("00:00:00", "");
                    jobId = reader.GetString(4);
                    int tblRows = 1;
                    int tblCols = 1;

                    Table tbl = new Table();
                    PlaceHolder1.Controls.Add(tbl);
                    for (int i = 0; i < tblRows; i++)
                    {
                        readerrowcount = readerrowcount + 1;
                        TableRow tr = new TableRow();
                        tr.CssClass = "rowStyle1";
                        for (int j = 0; j < tblCols; j++)
                        {
                            TableCell tc = new TableCell();
                            tc.CssClass = "cellStyle1";
                           System.Web.UI.WebControls.Label txtBox = new System.Web.UI.WebControls.Label();
                           txtBox.Text = "Job ID:" + jobId + "<br />" + "Job Designation:" + JobDesignation + "<br />" + "Job Description:" + JobDescription + "<br />" + "Vacancies:" + NoOfVacancies + "<br />" + "Ad Posted On:" + DatePosted + "<br />"+"";
                           tc.Controls.Add(txtBox);
                           tr.Cells.Add(tc);
                           System.Web.UI.WebControls.LinkButton lbView = new System.Web.UI.WebControls.LinkButton();
                           lbView.Text = "<br />" + "Apply for this Job";
                           lbView.Click += new EventHandler(lbView_Click);
                           lbView.OnClientClick = "return RedirectTo('" + id + "');";
                           lbView.ID = "linkButton" + readerrowcount;       
                           tc.Controls.Add(lbView);
                           tr.Cells.Add(tc);                         
                        }

                        tbl.Rows.Add(tr);

                    }
                    ViewState["dynamictable"] = true; 
                  } reader.NextResult();

            }

        } 

    }
    catch (SqlException exception)
    {
        MessageBox.Show(exception.Message, "warning!", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning);
    }
    finally
    {

        if(cmd.Connection.State==ConnectionState.Open)
        cmd.Connection.Close();
        cmd.Dispose();
    }
} 

    protected void lbView_Click(object sender, EventArgs e)
    {

    }

问题是当求职者点击FIND JOBS BUTTON时,作业列在但申请此工作根本没有解雇。

enter image description here

请帮忙。

1 个答案:

答案 0 :(得分:1)

你可以试试这个。

   LinkButton lnkButton = new LinkButton();
   lnkButton.ID = "lnkdynamicbutton"; 
   lnkButton.Text = "Apply for this job";
   lnkButton.Click += new System.EventHandler(lnkButton_Click);

protected void lnkButton_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "<script>alert('You Clicked me!!!')</script>");
}