使用HyperLink ASP.NET更新MySQL
本文关键字:更新 MySQL NET ASP HyperLink 使用 | 更新日期: 2023-09-27 17:51:09
我将所有数据存储在MySQL中,并在gridview中显示它的一些列。Gridview的删除按钮工作正常。我想编辑数据,所以创建了超链接。它导航另一个url,从MySQL数据库获取值,并设置文本框,文本区域。一切都很好,直到这个时候,但我尝试在新页面编辑值,它设置DB中的旧值。我怎样才能解决这个问题?
这是我的主页,有gridview和delete按钮,超链接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>
导航Url的页面
<%@ 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 yetkiliAdSoyad=@ayrintiYetkiliAdSoyad, gorusmeYapilanOkul = @ayrintiGorusmeYapilanOkul,"+
" veliAdSoyad = @ayrintiVeliAdSoyad ,veliTel = @ayrintiVeliTel,ikametAdres = @ayrintiIkametAdres ,ogrenciAdSoyad = @ayrintiOgrenciAdSoyad, " +
"ogrenciTel=@ayrintiOgrenciTel,ilgilendigiBolum = @ayrintiIlgilendigiBolum,verilenFiyat=@ayrintiVerilenFiyat,gorusmeSonucu=@ayrintiGorusmeSonucu,gorusmeNotlari=@ayrintiGorusmeNotlari", 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)
{
}
}
}
表单上的文本框在Page_Load
事件中使用其数据库默认值填充。第一次通过是好的;但是,当您单击"degistir"按钮时,会发生"回发",并且Page_Load
再次触发并且重新填充这些默认值,丢失已提交的任何更改。请记住,Page_Load
触发之前任何控件单击事件。
要解决这个问题,您需要检查Page_Load
中的IsPostback
,以防止加载器在更新时触发。如果IsPostback
为true,则禁止从数据库加载默认值。反过来,这应该允许使用表单上提供的更新数据进行更新。下面的内容:
// 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语句以包含WHERE子句,该子句将UPDATE限制为仅具有所需ID的记录。因为它是,你所有的记录将被更新!