asp.netvb.netformswebforms

How to call vb.net function inside form action attribute?


This is what I'm trying to implement:

<form id="form1" runat="server" action="<%= AllowedURL() %>">

The other variable is without =

<form id="form1" runat="server" action="<% AllowedURL() %>">

But both throws a 400 Bad Request.

In the url of the page I see this:

example.com/<%=%20AllowedURL()%20%>

Is there something to modify? Thanks in advance


Solution

  • There are restrictions for using <%=...%> constructs inside server tags. Solution is either using data binding or setting the form action in code behind.

    Solution 1: using data binding

    Protected Sub Page_Load() Handles Me.Load
        form1.DataBind()
    End Sub
    
    <form id="form1" runat="server" action="<%#AllowedURL()%>">
    

    Solution 2: setting action property in code behind

    Protected Sub Page_Load() Handles Me.Load
        If Not IsPostBack Then
            form1.Action = AllowedURL()
        End If
    End Sub
    
    <form id="form1" runat="server">