我有一个带有datagridview的网页,用户应该可以点击selectindexchanged(在这种情况下是“选择”按钮),显示他们在第二页上的标签中选择的内容的结果,还可以重定向他们到那个页面。这是我对vb.net的代码:
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TimeDateLabel.Text = String.Format("Today is {0:F}", DateTime.Now)
TextBox1.Focus()
End Sub
Protected Sub whosoncallButton_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles whosoncallButton.Click
Dim dt As New DataTable
Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim connectionString As String = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx"
Dim con As New SqlConnection(connectionString)
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "getoncall"
cmd.Parameters.AddWithValue("@subschedule", TextBox1.Text)
Try
da.SelectCommand = cmd
da.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
con.Dispose()
Catch ex As Exception
Response.Write("Error:" & ex.Message)
End Try
End Sub
Protected Sub clearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearButton.Click
TextBox1.Text = ""
GridView1.DataSource = Nothing
GridView1.DataBind()
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim row As GridViewRow = GridView1.SelectedRow
Response.Redirect("Default2.aspx?msg=" + row.Cells(1).Text)
End Sub
End Class
我还在事件处理程序的aspx页面中添加了一行。
我调试了这个但是无论我到目前为止尝试过什么,SelectIndexChanged事件都没有被触发。有人可以帮我吗?
谢谢
道格
答案 0 :(得分:0)
如果没有看到你的ASPX GridView,就无法确定为什么这不起作用,但我猜你没有正确生成你的选择按钮。你在GridView中设置了这个属性吗?
autogenerateselectbutton="True"
根据您在下面的评论中发布的内容,您可以将ASPX代码更改为以下任一项以使其正常工作:
使用您当前的ButtonField(Documentation):
<asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:ButtonField ButtonType="Button" Text="Select" CommandName="Select" />
</Columns>
</asp:GridView>
使用自动生成(Documentation):
<asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center"
onselectedindexchanged="GridView1_SelectedIndexChanged"
AutoGenerateSelectButton="True">
</asp:GridView>
答案 1 :(得分:0)
ButtonField的CommandName属性必须设置为“Select”才能触发SelectedIndexChanged事件。
<asp:ButtonField ButtonType="Button" Text="Select" CommandName="Select" />