トップ回答者
dwopdownlist 選択時にrowindexを取得する方法

質問
-
ASP.netのVBにて作成中です
gridviewのeditモードにわざわざいかずitemtemplateのみでgridviewを編集する方法を検討しています(VBにて)
dwopdownlistで選択を行った値をボタンをクリックすることによりバインドしているテキストボックスに送りアップデートしています。If (e.CommandName = "99") Thenmoji = Convert.ToInt32(e.CommandArgument)Dim opop As TextBox = DirectCast(GridView1.Rows(moji).FindControl("TextBox17"), TextBox)Dim wewe As DropDownList = DirectCast(GridView1.Rows(moji).FindControl("DropDownList2"), DropDownList)opop.Text = wewe.Text
GridView1.UpdateRow(moji, True)End Ifしかし、ボタンをクリックせずにdwopdownlistのselectchangedで上記の編集を満たすにはどうすればいいのでしょうか?dwopdownlist 選択時にrowindexを取得することはできないのでしょうか?
回答
-
DropDownList が html にレンダリングされると select 要素になります
が、それの onchange イベントに GetPostBackEventReference メソッド
を使ってポストバックするスクリプトを設定してはどうでしょう?GetPostBackEventReference メソッドの第2引数に RowIndex を設定す
れば、ポストバックしたとき RaisePostBackEvent メソッドの引数から
クリックされた行の RowIndex が取得できるはずです。
ClientScriptManager.GetPostBackEventReference メソッド (Control, String)
http://msdn.microsoft.com/ja-jp/library/ms153112.aspxIPostBackEventHandler.RaisePostBackEvent メソッド
http://msdn.microsoft.com/ja-jp/library/system.web.ui.ipostbackeventhandler.raisepostbackevent.aspx
DropDownList.SelectedIndexChanged イベントより RaisePostBackEvent
の方が後で起動されるので注意してください。
#dwopdownlist とは何でしょう? 大文字小文字を区別して、正しい
綴りで書いてください。 -
> わかりにくい文章による回答、ありがとうございます。
こちらでは質問者の方のレベルがどの程度なのかは、質問に書いてある
内容で想像するしかないです。アップしてあったコードからは初心者で
はなく、ある程度知識があるように見受けられました。なので、先のレ
スと、紹介したページのサンプルコードで解決できるであろうと思って
ましたが違ったようですね。こちらで作ったサンプルコードをアップしておきますので、これがやり
たいことと違うとか、不明な点があったら質問してください。DB はマイクロソフトが提供しているサンプル Northwind の Products
テーブルと Categories テーブルです。コードは C# ですが、C# が全く読めなければ、以下のサイトで変換し
てみてください。Convert C# to VB.NET
http://www.developerfusion.com/tools/convert/csharp-to-vb/<%@ Page Language="C#" %> <%@ Implements Interface="System.Web.UI.IPostBackEventHandler" %> <!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 GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string script = Page.ClientScript.GetPostBackEventReference(Page, e.Row.RowIndex.ToString()); DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1"); ddl.Attributes["onchange"] = script; } } public void RaisePostBackEvent(string eventArgument) { int rowIndex; if (Int32.TryParse(eventArgument, out rowIndex)) { GridView1.UpdateRow(rowIndex, true); } } </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 [ProductID], [ProductName], [CategoryID] FROM [Products]" UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [CategoryID] = @CategoryID WHERE [ProductID] = @ProductID"> <UpdateParameters> <asp:Parameter Name="ProductName" Type="String" /> <asp:Parameter Name="CategoryID" Type="Int32" /> <asp:Parameter Name="ProductID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" OnRowCreated="GridView1_RowCreated"> <Columns> <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" /> <asp:TemplateField HeaderText="ProductName" SortExpression="ProductName"> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductName") %>'> </asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CategoryID" SortExpression="CategoryID"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
- 編集済み SurferOnWww 2012年7月1日 2:01 一部追記
- 回答としてマーク 山本春海 2012年7月20日 8:57
すべての返信
-
DropDownList が html にレンダリングされると select 要素になります
が、それの onchange イベントに GetPostBackEventReference メソッド
を使ってポストバックするスクリプトを設定してはどうでしょう?GetPostBackEventReference メソッドの第2引数に RowIndex を設定す
れば、ポストバックしたとき RaisePostBackEvent メソッドの引数から
クリックされた行の RowIndex が取得できるはずです。
ClientScriptManager.GetPostBackEventReference メソッド (Control, String)
http://msdn.microsoft.com/ja-jp/library/ms153112.aspxIPostBackEventHandler.RaisePostBackEvent メソッド
http://msdn.microsoft.com/ja-jp/library/system.web.ui.ipostbackeventhandler.raisepostbackevent.aspx
DropDownList.SelectedIndexChanged イベントより RaisePostBackEvent
の方が後で起動されるので注意してください。
#dwopdownlist とは何でしょう? 大文字小文字を区別して、正しい
綴りで書いてください。 -
> わかりにくい文章による回答、ありがとうございます。
こちらでは質問者の方のレベルがどの程度なのかは、質問に書いてある
内容で想像するしかないです。アップしてあったコードからは初心者で
はなく、ある程度知識があるように見受けられました。なので、先のレ
スと、紹介したページのサンプルコードで解決できるであろうと思って
ましたが違ったようですね。こちらで作ったサンプルコードをアップしておきますので、これがやり
たいことと違うとか、不明な点があったら質問してください。DB はマイクロソフトが提供しているサンプル Northwind の Products
テーブルと Categories テーブルです。コードは C# ですが、C# が全く読めなければ、以下のサイトで変換し
てみてください。Convert C# to VB.NET
http://www.developerfusion.com/tools/convert/csharp-to-vb/<%@ Page Language="C#" %> <%@ Implements Interface="System.Web.UI.IPostBackEventHandler" %> <!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 GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string script = Page.ClientScript.GetPostBackEventReference(Page, e.Row.RowIndex.ToString()); DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1"); ddl.Attributes["onchange"] = script; } } public void RaisePostBackEvent(string eventArgument) { int rowIndex; if (Int32.TryParse(eventArgument, out rowIndex)) { GridView1.UpdateRow(rowIndex, true); } } </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 [ProductID], [ProductName], [CategoryID] FROM [Products]" UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [CategoryID] = @CategoryID WHERE [ProductID] = @ProductID"> <UpdateParameters> <asp:Parameter Name="ProductName" Type="String" /> <asp:Parameter Name="CategoryID" Type="Int32" /> <asp:Parameter Name="ProductID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" OnRowCreated="GridView1_RowCreated"> <Columns> <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" /> <asp:TemplateField HeaderText="ProductName" SortExpression="ProductName"> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductName") %>'> </asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CategoryID" SortExpression="CategoryID"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
- 編集済み SurferOnWww 2012年7月1日 2:01 一部追記
- 回答としてマーク 山本春海 2012年7月20日 8:57