使用HyperLink ASP.NET更新MySQL

荣誉

我将所有数据存储在MySQL中,并在gridview中显示一些列。Gridview的删除按钮可以正常工作。我想编辑创建的HyperLink数据。它导航另一个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>

浏览的网址页面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 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)
        {

        }
    }

    }
大卫·W

Page_Load事件中将使用其数据库默认值填充表单上的文本框第一次这样很好;但是,当您单击“ degistir”按钮时,将发生“回发”,并再次Page_Load触发重新填充这些默认值,从而丢失提交的所有更改。请记住,任何控件单击事件之前都会触发Page_Load

要解决此问题,您需要签IsPostbackPage_Load以防止加载程序在更新时启动。如果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的记录。照原样,您的所有记录都会更新!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用HyperLink ASP.NET更新MySQL

来自分类Dev

asp.net HyperLink无法找到网址

来自分类Dev

如何使用Asp.Net在客户端使用HyperLink字段将文本框值传递给查询字符串

来自分类Dev

在c#asp.net中单击了哪些HyperLink?

来自分类Dev

如何使用JavaScript和ASP.NET MVC更新图像?

来自分类Dev

使用Ajax的Asp.Net MVC部分视图更新

来自分类Dev

使用asp.net身份更新用户角色

来自分类Dev

使用sqlcommand在asp.net上检索更新的参数值

来自分类Dev

使用ASP.NET MVC更新数据库

来自分类Dev

使用WCF更新现有的asp.net项目

来自分类Dev

使用ASP.NET中的FileUpload更新SQL中的图像

来自分类Dev

Mysql 更新查询未运行 ASP.NET

来自分类Dev

使用MySQL和实体框架的ASP.NET身份

来自分类Dev

在 ASP.NET 中使用 MySqlCommand 的 MySQL 语法错误

来自分类Dev

Asp:HyperLink onmouseover和onmouseout

来自分类Dev

从Table ASP.NET更新

来自分类Dev

更新重定向ASP .net

来自分类Dev

更新 ASP.NET 标识

来自分类Dev

使用ASP.NET或Sharepoint

来自分类Dev

将ASP.NET HyperLink NavigateUrl设置为相对路径?

来自分类Dev

如何使用C#使用asp.net动态更新mongodb中相同ID的colums值

来自分类Dev

通过使用ASP.Net MVC 5中的DataTables行重新排序来更新值

来自分类Dev

在Asp.net MVC4中使用NHibernate更新对象

来自分类Dev

在ASP.NET MVC 4和KnockoutJS中使用主从表单时,视图未更新

来自分类Dev

使用OData补丁对ASP.NET MVC Web API进行部分更新

来自分类Dev

如何使用EF Core更新ASP.NET 5中的dbcontext脚手架?

来自分类Dev

使用实体框架更新ASP.NET MVC应用程序中的相关实体

来自分类Dev

选择的Jquery脚本不能与Asp.net更新面板一起使用?

来自分类Dev

使用ASP.NET Core MVC MultiSelectList更新多对多实体

Related 相关文章

热门标签

归档