[研究]ASP.NET,WebForm,TextBox1值自動更新到TextBox2,用OnTextChanged
2024-04-05
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19
********************************************************************************
TextBox1 輸入,TextBox2 自動更新成 TextBox1 ,用前端 JavaScript 做
Default21.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default21.aspx.cs"
Inherits="WebApplication1.Default21" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>"
SelectCommand="SELECT * FROM [MyTable]"
UpdateCommand="UPDATE [MyTable] SET [Field1] = @Field1, [Field2] = @Field2 WHERE [SN] = @SN">
<UpdateParameters>
<asp:Parameter Name="Field1" Type="String" />
<asp:Parameter Name="Field2" Type="String" />
<asp:Parameter Name="SN" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"
DefaultMode="Edit"
AllowPaging="True" DataKeyNames="SN"
>
<EditItemTemplate>
SN:<asp:Label ID="SNLabel1" runat="server" Text='<%# Eval("SN") %>' />
<br />
Field1:<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Field1") %>' OnTextChanged="TextBox1_TextChanged" AutoPostBack="true" />
<br />
Field2:<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Field2") %>' Enabled="false" />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" />
</EditItemTemplate>
</asp:FormView>
<asp:TextBox ID="TextBox3" runat="server" OnTextChanged="TextBox3_TextChanged" AutoPostBack="true"></asp:TextBox><br />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</form>
</body>
</html>
|
Default21.aspx.cs
using System;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Default21 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox TextBox1 = (TextBox)FormView1.FindControl("TextBox1");
TextBox TextBox2 = (TextBox)FormView1.FindControl("TextBox2");
TextBox2.Text = TextBox1.Text;
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
TextBox4.Text = TextBox3.Text;
}
}
}
|
測試正常
********************************************************************************
在 MasterPage 中,程式碼同上,實際測試正常。
(完)
相關
[研究]ASP.NET,WebForm,TextBox1值自動更新到TextBox2,用OnTextChanged
https://shaurong.blogspot.com/2024/04/aspnetwebformtextbox1textbox2ontextchan.html
[研究]ASP.NET,WebForm,TextBox1值自動更新到TextBox2,用JavaScript
https://shaurong.blogspot.com/2024/04/aspnetwebformtextbox1textbox2javascript.html
[研究]ASP.NET,WebForm,TextBox1值自動更新到TextBox2,用jQuery
https://shaurong.blogspot.com/2024/03/aspnetwebformtextbox1textbox2jquery.html

沒有留言:
張貼留言