单击按钮时无法使用注释填充我的更新面板

时间:2016-04-02 02:05:50

标签: c# asp.net webforms asp.net-ajax updatepanel

我想在点击按钮时将数据库中的注释添加到更新面板。 这是我的aspx页面 -

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default12.aspx.cs" Inherits="Default12" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Button ID="Button" runat="server" Text="Button" onclick="Button_Click" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Button" EventName = "Click"/>
        </Triggers>
        <ContentTemplate>
            <asp:PlaceHolder ID="CommentPlaceHolder" runat="server"></asp:PlaceHolder>
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

这是我的cs页面 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlClient;
using System.Data;

public partial class Default12 : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    string str;
    protected void Page_Load(object sender, EventArgs e)
    {
        str = @"Data source=INSPIRATION\SQLEXPRESS; Initial Catalog=ComputerPedia; Integrated security= true";
        con = new SqlConnection(str);
        if (con.State == ConnectionState.Closed)
            con.Open();
        cmd = new SqlCommand("Select Comment from CommentTable", con);
        dr = cmd.ExecuteReader();
    }
    protected void Button_Click(object sender, EventArgs e)
    {
            dr.Read();
            Response.Write(dr[0].ToString());
            Label l = new Label();
            l.ID = "l1";
            l.Text = dr[0].ToString();
            CommentPlaceHolder.Controls.Add(l);
            UpdatePanel1.Update();
    }
}

但这不起作用。连接没有问题,因为ic选择了来自工作网页的连接代码,并且查询也在sql中工作。请帮我解决一下这个。谢谢!

1 个答案:

答案 0 :(得分:0)

请使用修改后的代码完成您的操作。我已对修改过的部分进行了评论,并为其编写了合适的解释。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Button"/>  //modified
        </Triggers>
        <ContentTemplate>
            <asp:Button ID="Button" runat="server" Text="Button" onclick="Button_Click" /> // modified
            <asp:PlaceHolder ID="CommentPlaceHolder" runat="server"></asp:PlaceHolder>
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

这是您的aspx代码

这是你的.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlClient;
using System.Data;

public partial class Default12 : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    string str;
    protected void Page_Load(object sender, EventArgs e)
    {
        str = @"Data source=INSPIRATION\SQLEXPRESS; Initial Catalog=ComputerPedia; Integrated security= true";
        con = new SqlConnection(str);
        if (con.State == ConnectionState.Closed)
            con.Open();
        cmd = new SqlCommand("Select Comment from CommentTable", con);
        dr = cmd.ExecuteReader();
    }
    protected void Button_Click(object sender, EventArgs e)
    {

if (reader.HasRows) // to check whether there are comments or not in your database
{
      int commentIndexForLabel = 1;
    while (reader.Read())
    {      
       // Response.Write(dr[0].ToString()); 
            Label l = new Label();
            l.ID = "l" + commentIndexForLabel.ToString() ; //modified
            l.Text = (string)dr["Comment"]; //modified

            CommentPlaceHolder.Controls.Add(l);
            commentIndexForLabel ++;
            //UpdatePanel1.Update(); This is not required
    }
}
else
{
    // do whatever you want to do if there are no comments
}




    }
}

使用此代码并在此处分享您的输出。我希望事情能顺利运作