在asp:repeater

时间:2016-03-17 19:35:53

标签: c# asp.net repeater

我在数据库中有3个表

students_login, interview_events, interested_students,

student_login具有唯一性: - student_id,

interview_events具有唯一性: - id,

interest_students具有唯一性: - id,student_id,interview_id。

所以在接受采访.aspx页面上午显示采访。就像这个mdl-card。它有一个按钮(yes am going)。所以点击这个按钮我想通过存储student_id和interview_id来更新interested_student表。点击按钮后应该隐藏,一条消息显示为“成功”。

如果学生再次访问interview.aspx页面,那么卡片上的按钮就不会显示。

我尝试了很多东西但没有开始工作

我的aspx.cs页面

<asp:Repeater ID="Repeater1" runat="server">
  <ItemTemplate>
     <asp:Label ID="lbl_company_name" runat="server" Text='<%# Eval("company_name") %>'></asp:Label>
     <asp:Label ID="lbl_description" runat="server" Text='<%# Eval("description") %>'></asp:Label></b>
     <asp:Label ID="lbl_event_date" runat="server" Text='<%# "Date :"+Convert.ToDateTime(Eval("event_date")).ToString("dd/MMM/yyyy") %>'></asp:Label>
     <asp:Label ID="lbl_min_Ty" runat="server" Text=<%#"Minimum Aggregate of All Sems :"+ Eval("min_ty_per")+" %" %>></asp:Label>
     <asp:Label ID="lbl_min_hsc" runat="server" Text=<%#"Minimum Hsc Percentage : "+ Eval("min_hsc_per")+" %" %>></asp:Label>
     <asp:Label ID="lbl_min_ssc" runat="server" Text=<%#"Minumum SSC Percentage : "+ Eval("min_ssc_per")+" %" %>></asp:Label>
     <asp:Label ID="lbl_backlogs" runat="server" Text='<%#"Allowed Live Backlogs : "+ Eval("backlogs") %>'></asp:Label>
     <asp:HiddenField ID="hf_event_id" runat="server" Value=<%# Eval("id") %>></asp:HiddenField>
     <asp:HiddenField ID="hf_intreseted" runat="server" Value=<%# Eval("intrested") %>></asp:HiddenField>
     <asp:Label ID="lbl_already_intrested" runat="server" Text="You are Going To This Interview"></asp:Label>
     <asp:Button Text="Yes am Going" runat='server' ID="btn_intrested" OnClick="btn_Intrested_Click"></asp:Button>
     <asp:Label ID="lbl_status" runat="server" Text=""></asp:Label>
     <asp:Label ID="Label1" runat="server" Text=<%#Eval("intrested") %>></asp:Label>
  </ItemTemplate>
</asp:Repeater>

.cs页面:

public partial class InterviewDetails : System.Web.UI.Page
{
    //List<int> intrested_event = new List<int>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["student_username"] != null)
            {
                fetchInterviews();
            }
            else {
                Response.Redirect("studentLogin.aspx");
            }
        }
    }

    private void fetchInterviews()
    {
        //fetch both the tables interviews as well as interested
        DataSet ds = DataAccessLayer.fetchInterviewsEvents(Session["student_username"].ToString());
        DataTable dt_interview = ds.Tables[0];//interview table
        dt_interview.Columns.Add("intrested",typeof(bool));//added a column

        DataTable dt_intrested = ds.Tables[1];//interested table

        //this code is to insert true or false in the column we have created above 
        if (dt_intrested.Rows.Count > 0)//check if the student have not interested in any interview or coming for first time
        {
            foreach (DataRow dr_intrested in dt_intrested.Rows)
            {
                //interested_event.Add(Convert.ToInt32(dr_intrested.ItemArray[1]));
                foreach (DataRow dr_interview in dt_interview.Rows)
                {
                    //check if the interested table has the interiew_id
                    if (Convert.ToInt32(dr_intrested["interview_id"]) == Convert.ToInt32(dr_interview["id"]))
                    {
                        dr_interview["intrested"] = true;//student is intereseted
                    }
                    else
                    {
                        dr_interview["intrested"] = false;//student is not interested
                    }
                }
            }
        }
        else {
            foreach (DataRow dr_interview in dt_interview.Rows)
            {
                dr_interview["intrested"] = false;//make all value false if he is coming first time or not interested in avy interview
            }
        }
        Repeater1.DataSource = ds.Tables[0];
        Repeater1.DataBind();
    }

    protected void btn_Intrested_Click(object sender, EventArgs e)
    {
        var button = (Button)sender;
        var item = (RepeaterItem)button.NamingContainer;
        HiddenField hf_event_id = (HiddenField)item.FindControl("hf_event_id");
        int event_id = Convert.ToInt32(hf_event_id.Value);
        Label lbl_company_name = (Label)item.FindControl("lbl_company_name");
        Label lbl_description = (Label)item.FindControl("lbl_description");
        Label lbl_event_date = (Label)item.FindControl("lbl_event_date");
        Label lbl_min_Ty = (Label)item.FindControl("lbl_min_Ty");
        Label lbl_min_hsc = (Label)item.FindControl("lbl_min_hsc");
        Label lbl_min_ssc = (Label)item.FindControl("lbl_min_ssc");
        Label lbl_backlogs = (Label)item.FindControl("lbl_backlogs");
        Label lbl_status = (Label)item.FindControl("lbl_status");
        string error;
        //Label2.Text = event_id.ToString() +" "+ lbl_company_name.Text + " " + lbl_description.Text + " " + lbl_event_date.Text + " " + lbl_min_Ty.Text + " " + lbl_min_hsc.Text + " " + lbl_min_ssc.Text + " " + lbl_backlogs.Text;
        //submitting data to database
        if (DataAccessLayer.addStudentToIntrested(Session["student_username"].ToString(), event_id, out error))
        {
            if (error == null)
            {
                button.Visible = false;
                lbl_status.Text = "You are Going To This Interview";
            }
            else
            {
                lbl_status.Text = error;
            }
        }
        else
        {
            lbl_status.Text = "Some Error Occured";
        }
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            Button btn_intrested = (Button)e.Item.FindControl("btn_intrested");
            Label lbl_already_intrested = (Label)e.Item.FindControl("lbl_already_intrested");
            //HiddenField hf_event_id = (HiddenField)e.Item.FindControl("hf_event_id");
            HiddenField hf_intrested = (HiddenField)e.Item.FindControl("hf_event_id");
            //int event_id = Convert.ToInt32(hf_event_id.Value);
            bool intrested = Convert.ToBoolean(hf_intrested.Value);
            if (intrested == true)
            {
                btn_intrested.Visible = false;
                lbl_already_intrested.Visible = true;
            }
            else
            {
                btn_intrested.Visible = true;
                lbl_already_intrested.Visible = false;
            }

        }
    }
}

0 个答案:

没有答案