再次点击按钮时绑定HTML表格

时间:2015-05-01 07:44:50

标签: c# asp.net postback

我的Codebehind中有HTML表,它将在pageload上绑定数据。

现在我有一个按钮可以将数据插入数据库。我的问题是当我在点击按钮时插入数据,我的页面得到回发。我的html表数据将只显示我添加的旧数据而不是新数据。因为html表在pageload中首先绑定,在单词后我的控件将转到button_click事件。

我试图将ispostback放在page_load上但是第一次当页面加载html表时不显示任何数据。

我该如何解决这个问题。

  protected void Page_Load(object sender, EventArgs e)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select * from Ticket";
                conn.Open();

                SqlDataReader dr = cmd.ExecuteReader();

                // //Building an HTML string.
                StringBuilder html = new StringBuilder();

                //Table start.

                html.Append("<table id='tbldata' style='table-layout:fixed;' border='0'>");
                html.Append("<THEAD>");
                html.Append("<TR>");
                html.Append("<TH>Ticket</TH>");
                html.Append("<TH>Priority</TH>");
                html.Append("<TH>Status<div class='fildownarrow'></div></TH>");
                html.Append("<TH>Practice Name<div class='fildownarrow'></div></TH>");
                html.Append("<TH>Patient Name</TH>");
                html.Append("<TH>Assigned</TH>");
                html.Append("<TH>Subject</TH>");
                html.Append("<TH style='width:100px;><a href='#'><div class='filtericon'></div></a></TH>");
                html.Append("</TR>");
                html.Append("</THEAD>");

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        html.Append("<tbody>");
                        html.Append("<tr>");
                        html.Append("<td>" + dr["Ticket_no"] + "</td>");
                        html.Append("<td>" + dr["Priority"] + "</td>");
                        html.Append("<td>" + dr["Status"] + "</td>");
                        html.Append("<td>" + dr["Practice_Name"] + "</td>");
                        html.Append("<td>" + dr["Patient_Name"] + "</td>");
                        html.Append("<td>" + dr["Assign_User_Name"] + "</td>");
                        html.Append("<td>" + dr["Msg_Subject"] + "</td>");
                        html.Append("</tr>");
                        html.Append("</tbody>");
                    }
                }

                //Table end.
                html.Append("</table>");

                //Append the HTML string to Placeholder.
                PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });

                dr.Close();
                dr.Dispose();
            }

2 个答案:

答案 0 :(得分:2)

您应该以这种方式组织代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        LoadData();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    //Write data to database
    LoadData();
}

private void LoadData()
{
    using (SqlCommand cmd = new SqlCommand())
    {
        //Here goes your sql code that reads the database
    }
}

当然,更好的解决方案是使用类似GridView的控件并将数据直接绑定到它。

答案 1 :(得分:0)

如果在检查IsPostBack属性时页面加载时出现空表,我认为您正在检查它的值是否为真。所以表只会在回发时加载。您应检查IsPostBack属性值是否为false,以显示仅首页加载的内容。

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select * from Ticket";
            conn.Open();

            SqlDataReader dr = cmd.ExecuteReader();

            // //Building an HTML string.
            StringBuilder html = new StringBuilder();

            //Table start.

            html.Append("<table id='tbldata' style='table-layout:fixed;' border='0'>");
            html.Append("<THEAD>");
            html.Append("<TR>");
            html.Append("<TH>Ticket</TH>");
            html.Append("<TH>Priority</TH>");
            html.Append("<TH>Status<div class='fildownarrow'></div></TH>");
            html.Append("<TH>Practice Name<div class='fildownarrow'></div></TH>");
            html.Append("<TH>Patient Name</TH>");
            html.Append("<TH>Assigned</TH>");
            html.Append("<TH>Subject</TH>");
            html.Append("<TH style='width:100px;><a href='#'><div class='filtericon'></div></a></TH>");
            html.Append("</TR>");
            html.Append("</THEAD>");

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    html.Append("<tbody>");
                    html.Append("<tr>");
                    html.Append("<td>" + dr["Ticket_no"] + "</td>");
                    html.Append("<td>" + dr["Priority"] + "</td>");
                    html.Append("<td>" + dr["Status"] + "</td>");
                    html.Append("<td>" + dr["Practice_Name"] + "</td>");
                    html.Append("<td>" + dr["Patient_Name"] + "</td>");
                    html.Append("<td>" + dr["Assign_User_Name"] + "</td>");
                    html.Append("<td>" + dr["Msg_Subject"] + "</td>");
                    html.Append("</tr>");
                    html.Append("</tbody>");
                }
            }

            //Table end.
            html.Append("</table>");

            //Append the HTML string to Placeholder.
            PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });

            dr.Close();
            dr.Dispose();
        }
    }
}