トップ回答者
GridView でデータが無い場合(EmptyDataTemplate)の表示項目の値をとりたいのですが・・・

質問
-
現在、Visual Web Developer 2008 Express Edition(ASP.NET)のVBで開発を
行ってます。(開発初心者です。)GridViewでデータが存在しない場合、EmptyDataTemplate に Table を作成すれば
良いという所までは理解できたのですが、Table の中に 項目1(TextBox) 項目2(DropDownList) を入れたのですが、各々の
値を更新した時にプログラムを処理させたいのですが、項目1 の TextChanged と
項目2 の SelectedIndexChanged にどう記述をしていいか分かりません。具体的には、項目2のDropDownList を更新したら 項目2の値を 項目1のTextBoxに
入れたいというものになります。また、同様に項目1の値を更新後に項目2に入れたい
と考えています。本当に初歩的な事かと思いますが、どうかご指導の程宜しく
お願いいたします。GridView1 - EmptyDataTemplate
+-------+---------------------------------+
|項目1 | 項目2 |
+-------+---------------------------------+
|[____] |[____________________]▼]|
+-------+---------------------------------+↑の値にDropDownListで選択された値を表示したい。
回答
-
言葉で説明するのが大変なのでコードをアップしますが、以下のような感じで実現
可能と思います。
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.Parent.Parent;
TextBox tb = (TextBox)row.FindControl("TextBox1");
tb.Text = ddl.SelectedItem.Text;
}
</script><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers] WHERE ([CustomerID] = 'No id')">
</asp:SqlDataSource>
<asp:GridView ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1">
<EmptyDataTemplate>
<table style="width:100%;">
<tr>
<td>
項目1</td>
<td>
項目2</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="DropDownList1"
runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">aaaaaa</asp:ListItem>
<asp:ListItem Value="1">bbbbbb</asp:ListItem>
<asp:ListItem Value="2">cccccc</asp:ListItem>
<asp:ListItem Value="3">dddddd</asp:ListItem>
<asp:ListItem Value="4">eeeeee</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
</div>
</form>
</body>
</html>- 回答としてマーク 高橋 春樹 2009年11月11日 2:19
すべての返信
-
言葉で説明するのが大変なのでコードをアップしますが、以下のような感じで実現
可能と思います。
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.Parent.Parent;
TextBox tb = (TextBox)row.FindControl("TextBox1");
tb.Text = ddl.SelectedItem.Text;
}
</script><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers] WHERE ([CustomerID] = 'No id')">
</asp:SqlDataSource>
<asp:GridView ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1">
<EmptyDataTemplate>
<table style="width:100%;">
<tr>
<td>
項目1</td>
<td>
項目2</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="DropDownList1"
runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">aaaaaa</asp:ListItem>
<asp:ListItem Value="1">bbbbbb</asp:ListItem>
<asp:ListItem Value="2">cccccc</asp:ListItem>
<asp:ListItem Value="3">dddddd</asp:ListItem>
<asp:ListItem Value="4">eeeeee</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
</div>
</form>
</body>
</html>- 回答としてマーク 高橋 春樹 2009年11月11日 2:19