ListViewを使用してDBの値を表示・更新することはできたのですが
jikan1とjikan2の値を入れたら、その数値の合計がjikan3に自動で入力されるようにしたいのですが、そのようなことは可能なのでしょうか?
C#
1 <asp:TextBox ID="jikan2TextBox" runat="server" OnTextChanged="jikan2TextBox_TextChanged" AutoPostBack="true" Text='<%# Bind("jikan2") %>' />
のようにして、jikan2が変更されたらjikan2TextBox_TextChangedが呼ばれるところまで作れたのですが、jikan2TextBox_TextChanged内でjikan2TextBox.Textとしても値を取得することができずに困っています。
ListView内のコントロールから値を取得する方法、値を設定する方法をご存知の方は、教えていただけないでしょうか。
よろしくお願いします。
ちなみにわたしの環境は
windows7 Ultimate sp1 32bit
VS Cmmunity 2015
ASP.NET 4
ソースは以下の通りです。
・WebForm1.aspx
C#
1<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test.WebForm1" %> 2 3<!DOCTYPE html> 4 5<html xmlns="http://www.w3.org/1999/xhtml"> 6<head runat="server"> 7<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8 <title></title> 9</head> 10<body> 11 <form id="form1" runat="server"> 12 <div> 13 <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1"> 14 <EditItemTemplate> 15 <tr style="background-color: #999999;"> 16 <td> 17 <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" /> 18 <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="キャンセル" /> 19 </td> 20 <td> 21 <asp:Label ID="IdLabel1" runat="server" Text='<%# Eval("Id") %>' /> 22 </td> 23 <td> 24 <asp:TextBox ID="jikan1TextBox" runat="server" Text='<%# Bind("jikan1") %>' /> 25 </td> 26 <td> 27 <asp:TextBox ID="jikan2TextBox" runat="server" OnTextChanged="jikan2TextBox_TextChanged" AutoPostBack="true" Text='<%# Bind("jikan2") %>' /> 28 </td> 29 <td> 30 <asp:TextBox ID="jikan3TextBox" runat="server" Text='<%# Bind("jikan3") %>' /> 31 </td> 32 </tr> 33 </EditItemTemplate> 34 <EmptyDataTemplate> 35 <table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;"> 36 <tr> 37 <td>データは返されませんでした。</td> 38 </tr> 39 </table> 40 </EmptyDataTemplate> 41 <ItemTemplate> 42 <tr style="background-color: #E0FFFF;color: #333333;"> 43 <td> 44 <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編集" /> 45 </td> 46 <td> 47 <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' /> 48 </td> 49 <td> 50 <asp:Label ID="jikan1Label" runat="server" Text='<%# Eval("jikan1") %>' /> 51 </td> 52 <td> 53 <asp:Label ID="jikan2Label" runat="server" Text='<%# Eval("jikan2") %>' /> 54 </td> 55 <td> 56 <asp:Label ID="jikan3Label" runat="server" Text='<%# Eval("jikan3") %>' /> 57 </td> 58 </tr> 59 </ItemTemplate> 60 <LayoutTemplate> 61 <table runat="server"> 62 <tr runat="server"> 63 <td runat="server"> 64 <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;"> 65 <tr runat="server" style="background-color: #E0FFFF;color: #333333;"> 66 <th runat="server"></th> 67 <th runat="server">Id</th> 68 <th runat="server">jikan1</th> 69 <th runat="server">jikan2</th> 70 <th runat="server">jikan3</th> 71 </tr> 72 <tr id="itemPlaceholder" runat="server"> 73 </tr> 74 </table> 75 </td> 76 </tr> 77 <tr runat="server"> 78 <td runat="server" style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF"></td> 79 </tr> 80 </table> 81 </LayoutTemplate> 82 </asp:ListView> 83 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table]" UpdateCommand="UPDATE [Table] SET jikan1 = @jikan1, jikan2 = @jikan2, jikan3 = @jikan3 WHERE (Id = @Id)"> 84 <UpdateParameters> 85 <asp:Parameter Name="jikan1" /> 86 <asp:Parameter Name="jikan2" /> 87 <asp:Parameter Name="jikan3" /> 88 <asp:Parameter Name="Id" /> 89 </UpdateParameters> 90 </asp:SqlDataSource> 91 </div> 92 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 93 </form> 94</body> 95</html> 96
・WebForm1.aspx.cs
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Web; 5using System.Web.UI; 6using System.Web.UI.WebControls; 7 8namespace test 9{ 10 public partial class WebForm1 : System.Web.UI.Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 15 } 16 17 protected void jikan2TextBox_TextChanged(object sender, EventArgs e) 18 { 19 jikan3TextBox.Text = jikan1TextBox.Text + jikan2TextBox.Text; //ここでエラー 20 } 21 } 22}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。