简体   繁体   English

使用HyperLink ASP.NET更新MySQL

[英]Update MySQL using HyperLink ASP.NET

I store all data in MySQL and display it's some colums in gridview. 我将所有数据存储在MySQL中,并在gridview中显示一些列。 Gridview's delete button works fine. Gridview的删除按钮可以正常工作。 I would like to edit datas so created HyperLink. 我想编辑创建的HyperLink数据。 it navigates another url and gets values from MySQL DB and sets textboxes, textareas. 它导航另一个URL并从MySQL DB获取值,并设置文本框,文本区域。 Everything is fine until this time but I try to edit values in new page it sets old values in DB. 到目前为止,一切都很好,但是我尝试在新页面中编辑值,它在数据库中设置了旧值。 How i can solve this problem? 我该如何解决这个问题?

This my Main Page which has gridview and delete button, hyperlink KayitGoruntule.aspx; 我的主页上有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>

Navigated Url's page KayitAyrinti.aspx; 浏览的网址页面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>

This class is which shows values and try to update with degistir_Click, KayitAyrinti.aspx.cs; 此类显示值并尝试使用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)
        {

        }
    }

    }

The textboxes on your form are being populated with their database defaults in the Page_Load event. Page_Load事件中,将使用其数据库默认值填充表单上的文本框。 This is fine the first time through; 第一次这样很好。 however, when you click your 'degistir' button, a "postback" occurs, and Page_Load fires again and repopulates those defaults, losing whatever changes were submitted. 但是,当您单击“ degistir”按钮时,将发生“回发”,并且Page_Load 再次触发并重新填充这些默认值,从而丢失提交的所有更改。 Keep in mind that Page_Load fires before any control click events. 请记住, Page_Load 任何控件单击事件之前触发。

To fix this, you need to check for IsPostback in Page_Load to prevent your loader from firing on the update. 要解决此问题,您需要在Page_Load检查IsPostback ,以防止加载程序在更新时触发。 If IsPostback is true, inhibit the load of the defaults from your database. 如果IsPostback为true,则禁止从数据库中加载默认值。 This, in turn, should allow the update to occur with the updated data provided on your form. 反过来,这应该允许使用表单上提供的更新数据进行更新。 Something along these lines: 遵循以下原则:

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

Also, be sure to modify your UPDATE statement to include a WHERE clause that limits the UPDATE to only that record with the desired ID. 另外,请确保修改UPDATE语句以包含WHERE子句,该子句将UPDATE限制为仅具有所需ID的记录。 As it is, ALL your records will be updated! 照原样,您的所有记录都会更新!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM