2015-05-29 143 views
2

我将所有数据存储在MySQL中,并在gridview中显示它的一些列。 Gridview的删除按钮工作正常。我想编辑创建HyperLink的数据。它导航另一个URL并从MySQL DB获取值并设置文本框textareas。一切都很好,直到这一次,但我尝试编辑新页面中的值,它在数据库中设置旧值。我能如何解决这个问题?使用超链接更新MySQL ASP.NET

这是我的主页,它有gridview和删除按钮,超链接KayitGoruntule.aspx;

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="KayitGoruntule.aspx.cs" Inherits="gop.KayitGoruntule" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <style type="text/css"> 
     .string { 
      text-align: left; 
     } 
    </style> 
</head> 
<body> 
     <center><asp:Image ID="Banner" runat="server" ImageUrl="images/logo.png" Height="88px" Width="509px"></asp:Image></center> 

    <form id="form1" runat="server"> 
     <br /> 
    <div> 
     <asp:GridView ID="gvMysqlData" runat="server" CssClass="string" 
      SelectedIndex="0" DataKeyNames="id" 
      ShowHeaderWhenEmpty="True" OnRowDeleting="gvMysqlData_RowDeleting" Height="95px" Width="492px" CellPadding="4" ForeColor="#333333" GridLines="None">  
      <AlternatingRowStyle BackColor="White" /> 
<Columns> 
        <asp:TemplateField HeaderText="" ItemStyle-Width="15%"> 
         <ItemTemplate> 
          <asp:HyperLink ID="hpr1" runat="server" NavigateUrl='<%# string.Format("KayitAyrinti.aspx?id={0}",Eval("id")) %>'> 
          <img src="images/edit.png" /> 
          </asp:HyperLink> 
           </ItemTemplate> 
         <ItemStyle Width="15%" /> 
        </asp:TemplateField> 
        <asp:CommandField ShowDeleteButton="True" /> 
      </Columns> 
      <EditRowStyle BackColor="#2461BF" /> 
      <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#EFF3FB" /> 
      <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
      <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
      <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
      <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
      <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
     </asp:GridView> 
    </div> 
    </form> 
</body> 
</html> 

Navigated Url的页面KayitAyrinti.aspx;

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="KayitAyrinti.aspx.cs" Inherits="gop.KayitAyrinti" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <style type="text/css"> 
     .auto-style2 { 
      height: 23px; 
      width: 217px; 
     } 

     .auto-style1 { 
      height: 23px; 
      width: 271px; 
     } 

.degistir-button { 
background:#7ab752; 
margin:1px auto 0px; 
text-align:right; 
color:#FFF; 
/*border:none; 
border-top-left-radius:4px; 
border-bottom-left-radius:4px;*/ 
-webkit-transition:background 0.5s; 
} 
.degistir-button:hover { 
background:#DC3F42 #81c356; 
} 
.kaydet-button { 
background:#7ab752; 
margin:auto; 
margin-right:4px; 
margin-left:60px; 
text-align:right; 
color:#FFF; 
/* border:none; 
border-top-left-radius:40px; 
border-bottom-left-radius:40px; 
*/ 
-webkit-transition:background 0.5s; 

} 
.kaydet-button:hover { 
background:#DC3F42 #81c356; 
} 
</style> 
</head> 
<body> 
    <center><asp:Image ID="Banner" runat="server" ImageUrl="images/logo.png" Height="88px" Width="509px"></asp:Image></center> 
    <form id="register" runat="server"> 
      <br /> 
     <div> 
     <table align="center"> 
      <tr> 
       <td class="auto-style1" > 
        <asp:Label ID="lbl_ayrintiYetkiliAdSoyad" runat="server" Text="Yetkili Adı Soyad :" Font-Bold="True" Font-Names="Book Antiqua" ></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <asp:TextBox ID="ayrintiYetkiliAdSoyad_txt" runat="server" Width="200px"></asp:TextBox> 
       </td> 



      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_ayrintiGorusmeYapilanOkul" runat="server" Text="Görüşme Yapılan Okul :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <asp:RadioButtonList ID="RadioButtonList_ayrintiGorusmeYapilanOkul" runat="server" Width="174px"> 
         <asp:ListItem>Seyrantepe Şube 1</asp:ListItem> 
         <asp:ListItem>Seyrantepe Şube 2</asp:ListItem> 
        </asp:RadioButtonList> 
       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_ayrintiveliAdSoyad" runat="server" Text="Veli Adı Soyadı :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <asp:TextBox ID="ayrintiVeliAdiSoyadi_txt" runat="server" Width="200px"></asp:TextBox> 

       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_veliTel" runat="server" Text="Veli Telefon Numarası :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <asp:TextBox ID="ayrintiVeliTel_txt" runat="server" Width="200px"></asp:TextBox> 
       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_ikametAdres" runat="server" Text="İkamet Adresi :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <textarea id="ayrintiIkametAdres_txt" rows="5" cols="26" runat="server"></textarea> 
       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_ogrenciAdSoyad" runat="server" Text="Öğrenci Adı Soyadı :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <asp:TextBox ID="ayrintiOgrenciAdSoyad_txt" runat="server" Width="200px"></asp:TextBox> 
       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_ogrenciTel" runat="server" Text="Öğrenci Telefon Numarası :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <asp:TextBox ID="ayrintiOgrenciTel_txt" runat="server" Width="200px"></asp:TextBox> 
       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_ilgilendigiBolum" runat="server" Text="İlgilendiği Bölüm :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <asp:RadioButtonList ID="RadioButtonList_ayrintiIlgilendigiBolum" runat="server" Width="174px"> 
         <asp:ListItem>Hemşire Yardımcılığı</asp:ListItem> 
         <asp:ListItem>Sağlık Bakım Teknisyenliği</asp:ListItem> 
         <asp:ListItem>Anadolu Lisesi</asp:ListItem> 
        </asp:RadioButtonList>    
       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_verilenFiyat" runat="server" Text="Verilen Fiyat :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <asp:TextBox ID="ayrintiVerilenFiyat_txt" runat="server" Width="200px"></asp:TextBox> 
       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_gorusmeSonucu" runat="server" Text="Görüşme Sonucu :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <asp:TextBox ID="ayrintiGorusmeSonucu_txt" runat="server" Width="200px"></asp:TextBox> 
       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="lbl_gorusmeNotlari" runat="server" Text="Görüşme Notları :" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 
       </td> 
       <td class="auto-style2"> 
        <textarea id="ayrintiGorusmeNotlari_txt" rows="5" cols="26" runat="server"></textarea> 
       </td> 

      </tr> 

      <tr> 
       <td class="auto-style1"> 
       <asp:Label ID="Label1" runat="server" Text="" Font-Bold="True" Font-Names="Book Antiqua"></asp:Label> 

       </td> 
       <td class="auto-style2"> 

        <asp:button id="degistir" runat="server" text="Değiştir" class="degistir-button" OnClick="degistir_Click"/> 
        <asp:Button ID="kaydet" runat="server" Text="Kaydet" CssClass="kaydet-button" OnClick="kaydet_Click" /> 
       </td> 
      </tr> 
     </table> 
    </div> 
    </form> 

</body> 
</html> 

这个类是表示值,并尝试与degistir_Click更新, KayitAyrinti.aspx.cs;

using MySql.Data.MySqlClient; 
using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace gop 
{ 
    public partial class KayitAyrinti : System.Web.UI.Page 
    { 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (Session["kullanici"] != null) 
      { 
       Response.Write("Hoşgeldiniz..." + Session["kullanici"]); 
       Response.Redirect("KayitAyrinti.aspx"); 
      } 
      else 
      { 
       // Response.Write("Giriş Yapınız."); 
      } 
      string connectionString = "xxx;Database=xxx;Uid=xxxx;Pwd=xxx;"; 
      using (MySqlConnection cn = new MySqlConnection(connectionString)) 
      { 
       string[] keys = Request.QueryString.GetValues("id"); 
       String id = keys[0]; 
       MySqlCommand cmd = new MySqlCommand("select id, yetkiliAdSoyad,gorusmeYapilanOkul,veliAdSoyad, veliTel, ikametAdres, ogrenciAdSoyad, ogrenciTel, ilgilendigiBolum,verilenFiyat,gorusmeSonucu,gorusmeNotlari from Kayitlar where id=" + id + "", cn); 
       try 
       { 
        cn.Open(); 
        using (MySqlDataReader reader = cmd.ExecuteReader()) 
        { 
         while (reader.Read()) 
         { 

          ayrintiYetkiliAdSoyad_txt.Text = (reader["yetkiliAdSoyad"].ToString()); 
          RadioButtonList_ayrintiIlgilendigiBolum.SelectedValue = (reader["gorusmeYapilanOkul"].ToString()); 
          ayrintiVeliAdiSoyadi_txt.Text = (reader["veliAdSoyad"].ToString()); 
          ayrintiVeliTel_txt.Text = (reader["veliTel"].ToString()); 
          ayrintiIkametAdres_txt.InnerText = (reader["ikametAdres"].ToString()); 
          ayrintiOgrenciAdSoyad_txt.Text= (reader["ogrenciAdSoyad"].ToString()); 
          ayrintiOgrenciTel_txt.Text = (reader["ogrenciTel"].ToString()); 
          RadioButtonList_ayrintiIlgilendigiBolum.SelectedValue= (reader["ilgilendigiBolum"].ToString()); 
          ayrintiVerilenFiyat_txt.Text= (reader["verilenFiyat"].ToString()); 
          ayrintiGorusmeSonucu_txt.Text= (reader["gorusmeSonucu"].ToString()); 
          ayrintiGorusmeNotlari_txt.InnerText = (reader["gorusmeNotlari"].ToString()); 



         } 

        } 
       } 
       catch (Exception ex) 
       { 

       } 
      } 

     } 

     protected void degistir_Click(object sender, EventArgs e) 
     { 
      string connectionString = "Server=xxx;Database=xxx;Uid=xxx;Pwd=xxx;"; 
      using (MySqlConnection cn = new MySqlConnection(connectionString)) 
      { 
       string[] keys = Request.QueryString.GetValues("id"); 
       String id = keys[0]; 
       cn.Open(); 






       MySqlCommand komut = new MySqlCommand("UPDATE Kayitlar SET [email protected], gorusmeYapilanOkul = @ayrintiGorusmeYapilanOkul,"+ 
       " veliAdSoyad = @ayrintiVeliAdSoyad ,veliTel = @ayrintiVeliTel,ikametAdres = @ayrintiIkametAdres ,ogrenciAdSoyad = @ayrintiOgrenciAdSoyad, " + 
       "[email protected],ilgilendigiBolum = @ayrintiIlgilendigiBolum,[email protected],[email protected],[email protected]", cn); 

       komut.Parameters.AddWithValue("@ayrintiYetkiliAdSoyad", ayrintiYetkiliAdSoyad_txt.Text); 
       komut.Parameters.AddWithValue("@ayrintiGorusmeYapilanOkul", RadioButtonList_ayrintiGorusmeYapilanOkul.SelectedValue); 
       komut.Parameters.AddWithValue("@ayrintiVeliAdSoyad", ayrintiVeliAdiSoyadi_txt.Text); 
       komut.Parameters.AddWithValue("@ayrintiVeliTel", ayrintiVeliTel_txt.Text); 
       komut.Parameters.AddWithValue("@ayrintiIkametAdres", ayrintiIkametAdres_txt.InnerText); 
       komut.Parameters.AddWithValue("@ayrintiOgrenciAdSoyad", ayrintiOgrenciAdSoyad_txt.Text); 
       komut.Parameters.AddWithValue("@ayrintiOgrenciTel", ayrintiOgrenciTel_txt.Text); 
       komut.Parameters.AddWithValue("@ayrintiIlgilendigiBolum", RadioButtonList_ayrintiIlgilendigiBolum.SelectedValue); 
       komut.Parameters.AddWithValue("@ayrintiVerilenFiyat", ayrintiVerilenFiyat_txt.Text); 
       komut.Parameters.AddWithValue("@ayrintiGorusmeSonucu", ayrintiGorusmeSonucu_txt.Text); 
       komut.Parameters.AddWithValue("@ayrintiGorusmeNotlari", ayrintiGorusmeNotlari_txt.InnerText); 

       komut.ExecuteNonQuery(); 

       Response.Redirect("KayitGoruntule.aspx"); 
       komut.Dispose(); 




      } 
     } 



     protected void kaydet_Click(object sender, EventArgs e) 
     { 

     } 
    } 

    } 
+0

初步审查后,它看起来像你的UPDATE语句没有WHERE子句来限制受影响的记录只有指定的ID;因此,您的所有记录似乎将更新与各种文本框中的值.... –

+1

我忘了在这里添加它,但它有实际代码中的where子句。答案是正常工作,这是下降。感谢您的关注。 – Onur

回答

0

表单上的文本框正在使用Page_Load事件中的数据库默认值填充。这是第一次通过;然而,当您点击'degistir'按钮时,会发生“回传”,并且Page_Load再次触发重新填充这些默认值,失去提交的所有更改。请记住Page_Load在之前触发任何控制点击事件。

要解决此问题,您需要检查的Page_Load以防止加载器触发更新。如果IsPostback为真,禁止从数据库中加载默认值。这反过来会使更新与您的表单上提供的更新数据一起发生。沿着这些路线的东西:

 // code snipped... 
     if (Session["kullanici"] != null) 
     { 
      Response.Write("Hoşgeldiniz..." + Session["kullanici"]); 
      Response.Redirect("KayitAyrinti.aspx"); 
     } 
     else 
     { 
      // Response.Write("Giriş Yapınız."); 
     } 
     if (!IsPostback) // <-- Add this check 
     { 
      string connectionString = "xxx;Database=xxx;Uid=xxxx;Pwd=xxx;"; 
      using (MySqlConnection cn = new MySqlConnection(connectionString)) 
      { 
       string[] keys = Request.QueryString.GetValues("id"); 
       /// rest of code snipped 

另外,一定要修改您的UPDATE语句,包括限制UPDATE只与所需的ID记录WHERE子句。事实上,所有的记录都会更新!