【问题标题】:post back to the same page not working asp.net回发到同一页面不起作用 asp.net
【发布时间】:2011-11-23 14:02:36
【问题描述】:

在我的任何特定用户帐户的 user.aspx 页面中,将所有其他用户名/图像显示为数据列表中的链接以及登录的特定用户信息。现在我希望如果我单击任何其他用户链接用户信息应该出现在同一个 user.aspx 页面上。我在页面加载时使用此代码

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Session("UserName") Is Nothing Then
        Response.Redirect("login.aspx")
    Else
        If Not (IsPostBack) Then
            binddata()
            binddata1()
            binddata2()
        End If
    End If
End Sub


     Sub binddata()
    Dim mycommand As New SqlCommand("SELECT * FROM details where id = @id", con)
    mycommand.Parameters.Add("@id", SqlDbType.Int)
    mycommand.Parameters("@id").Value = Convert.ToInt32(Request.QueryString("id"))
    con.Open()
    Data.DataSource = mycommand.ExecuteReader
    Data.DataBind()
    con.Close()

End Sub
Sub binddata1()

    Dim mycommand As New SqlCommand("SELECT * FROM details where id = @id", con)
    mycommand.Parameters.Add("@id", SqlDbType.Int)
    mycommand.Parameters("@id").Value = Convert.ToInt32(Request.QueryString("id"))
    con.Open()
    nameimage.DataSource = mycommand.ExecuteReader
    nameimage.DataBind()
    con.Close()
End Sub
Sub binddata2()

    Dim mycommand As New SqlCommand("SELECT * FROM details ", con)
    con.Open()
   user.DataSource = mycommand.ExecuteReader
    user.DataBind()
    con.Close()
End Sub

这个脚本用于数据列表

  <asp:DataList ID="custom" runat="server" DataKeyField="Name" RepeatColumns="1" >
          <ItemTemplate>
          <div><table>
             <tr><asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%#   GetImageURL(Eval("Pic")) %>' PostBackUrl='<%# Eval("id", "user.aspx?id={0}") %>'/></tr>
            <tr><td align="left" valign="top" style="width:90px"><asp:LinkButton ID="name" runat="server" ForeColor="Blue" Text='<%#Container.DataItem("Name")%>'></asp:LinkButton></td></tr>
               </table></div></ItemTemplate>
</asp:DataList>

此代码在第一次检查/单击时运行良好。当我单击其中一个用户时,它会加载用户的详细信息。但在那之后,当我单击另一个用户时,它不会加载该用户的详细信息。我想显示:一个用户登录,然后在他的页面上有其他用户的图像列表。当我单击其中任何一个时,特定的详细信息应加载到适当的位置。帮我解决这个问题

【问题讨论】:

  • 我不知道你想要达到什么目标以及你为什么想要?

标签: asp.net data-binding postback


【解决方案1】:

您正在执行数据绑定之前检查第一页加载。删除此检查或检查在它之前触发回发的控件名称。

【讨论】:

  • 怎么做,你能解释一下吗
  • Page_Load子中删除If Not (IsPostBack) Then支票
【解决方案2】:

Yuriy 的回答应该有效,但另一种方法是使用 CommandArgument 而不是 PostBackUrl。您将需要为您的 DataList ItemCommand 设置一个事件处理程序。 CommandArgument 值将是您的 ID 值。您的事件处理程序将获取该 ID 并将其绑定到您的数据显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多