2015-05-29 144 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子句。事實上,所有的記錄都會更新!