【问题标题】:Simple LinkButton Postback简单的 LinkBut​​ton 回发
【发布时间】:2012-09-04 17:42:22
【问题描述】:

这一定是有史以来最简单的问题。我试图使用带有“postbackurl”的链接按钮将值发布到另一个页面。它适用于一个按钮,但它是一个链接按钮是空白的。我不想使用查询字符串。有什么想法吗?

default.aspx:

   <asp:LinkButton ID="LinkButton" postbackurl="~/2.aspx" Text="ThisIsATest" runat="server">ThisIsATest</asp:LinkButton>

2.aspx:

    Dim test = Request.Form("LinkButton")

    Response.Write(test)

【问题讨论】:

    标签: asp.net asp.net-mvc vb.net linq


    【解决方案1】:

    试试这个代码

    var text = ((LinkButton)PreviousPage.FindControl("LinkButton")).Text;
    

    【讨论】:

    • 还是什么都没有。它在 contentplaceholder(content1) 中的 gridview (gridview1) 中,这有关系吗?我做过研究,但它只会让我更加困惑。
    • 您需要做的不仅仅是在 GridView 中查找 LinkBut​​ton。您还需要引用 LinkBut​​ton 所在的正确行。
    • 正是你在 id 中引用了你的网格和行
    • 点击链接时如何动态引用网格和行?
    • 你已经嵌套了 UniqueId ,UniqueID 使用冒号作为父控件到子控件的分隔符,
    【解决方案2】:

    使用 PreviousPage.FindControl 获取控件引用

    Dim lb As LinkButton = DirectCast(PreviousPage.FindControl("LinkButton"), LinkButton)
    
    If lb IsNot Nothing Then
         Response.Write(test.ID) ' display linkbutton ID
         Response.Write(test.Text) ' display linkbutton Text
    End If   
    

    【讨论】:

    • 它在一个网格视图 (gridview1) 中,它在一个 contentplaceholder(content1) 中,这有关系吗?
    • 是的,这很重要,请将此作为PreviousPage.FindControl("ContentPlaceHolderID$GridViewID$LinkButton"), LinkButton)
    • 您可以使用 FindControl 动态获取 ContentPlaceHolder 和 GridView 的 id。 PreviousPage.FindControl("Content1").FindControl("GridView1").FindControl("LinkBut​​ton1") 可以使用,但是如果任何 FindControl 调用返回 null/nothing,代码就会崩溃
    • @Scott 对不起 scott,如果我们使用 PreviousPage.FindControl 也没关系。
    • 我不断收到 NullReferenceException,即使我的列/行都不为空。到目前为止,这个页面上几乎所有的东西都试过了,所以这完全是针对的。
    【解决方案3】:

    试试:

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton1.Click
        Response.Redirect("2.aspx?id=" + somestring)
    End Sub
    

    【讨论】:

    • “我不想使用查询字符串。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    相关资源
    最近更新 更多