2016-04-02 33 views
0

tıklatmayla ilgili yorumlarla doldurulamıyor Veritabanımdan bir düğmeyi tıklatıp güncellemeye eklemek istiyorum. bu benim cs Güncelleme panelim, bir düğmeyi tıklamak üzere

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

sayfası- olduğu sayfası-

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

benim aspx ama bu çalışmıyor. Bağlantıyla ilgili bir sorun yoktur, çünkü çalışan bir web sayfasından bağlantı kodu kullanıldı ve sorgu ayrıca sql'de çalışıyor. lütfen bana bununla yardım edin. Teşekkürler!

cevap

0

Lütfen işleminizi tamamlamak için değiştirilmiş kodu kullanın. Değiştirilmiş bölümü yorumladım ve bunun için uygun bir açıklama yazdım.

<!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> 

Bu

Sizin aspx kod oldu

İşte .cs kod

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 
} 




    } 
} 

kullanın bu kod gelir ve buradan çıkış paylaşır. Umarım işler iyi çalışacaktır umarım